Skip to content

Commit 17ca5ef

Browse files
committed
Add non repeating logger
1 parent 57fd632 commit 17ca5ef

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/sagemaker/config/config_utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import logging
2020
import sys
21+
from types import MethodType
2122

2223

2324
def get_sagemaker_config_logger():
@@ -46,7 +47,7 @@ def get_sagemaker_config_logger():
4647
# if a handler is being added, we dont want the root handler to also process the same events
4748
sagemaker_config_logger.propagate = False
4849

49-
return sagemaker_config_logger
50+
return non_repeating_logger(sagemaker_config_logger)
5051

5152

5253
def _log_sagemaker_config_single_substitution(source_value, config_value, config_key_path: str):
@@ -197,3 +198,25 @@ def _log_sagemaker_config_merge(
197198
else:
198199
# nothing was specified in the config and nothing is being automatically applied
199200
logger.debug("Skipped value because no value defined\n config key = %s", config_key_path)
201+
202+
203+
def non_repeating_logger(logger: logging.Logger) -> logging.Logger:
204+
"""Patch the logger to remove repeating info logs.
205+
206+
Args:
207+
logger (logging.Logger): the logger to be patched
208+
209+
Returns:
210+
(logging.Logger): the patched logger
211+
"""
212+
_caches = set()
213+
_old_impl = logger.info
214+
215+
def _new_impl(_, msg, *args, **kwargs):
216+
key = f"{msg}:{args}"
217+
if key not in _caches:
218+
_old_impl(msg, *args, **kwargs)
219+
_caches.add(key)
220+
221+
logger.info = MethodType(_new_impl, logger)
222+
return logger

0 commit comments

Comments
 (0)