|
15 | 15 | These utils may be used inside or outside the config module. |
16 | 16 | """ |
17 | 17 | from __future__ import absolute_import |
| 18 | +from collections import deque |
18 | 19 |
|
19 | 20 | import logging |
20 | 21 | import sys |
@@ -200,26 +201,31 @@ def _log_sagemaker_config_merge( |
200 | 201 | logger.debug("Skipped value because no value defined\n config key = %s", config_key_path) |
201 | 202 |
|
202 | 203 |
|
203 | | -def non_repeating_log_factory(logger: logging.Logger, method: str) -> Callable: |
| 204 | +def non_repeating_log_factory(logger: logging.Logger, method: str, cache_size=100) -> Callable: |
204 | 205 | """Create log function that filters the repeated messages. |
205 | 206 |
|
| 207 | + By default. It only keeps track of last 100 messages, if a repeated |
| 208 | + message arrives after the ``cache_size`` messages, it will be displayed. |
| 209 | +
|
206 | 210 | Args: |
207 | 211 | logger (logging.Logger): the logger to be used to dispatch the message. |
208 | 212 | method (str): the log method, can be info, warning or debug. |
| 213 | + cache_size (int): the number of last log messages to keep in cache. |
| 214 | + Default to 100 |
209 | 215 |
|
210 | 216 | Returns: |
211 | 217 | (Callable): the new log method |
212 | 218 | """ |
213 | 219 | if method not in ["info", "warning", "debug"]: |
214 | 220 | raise ValueError("Not supported logging method.") |
215 | 221 |
|
216 | | - _caches = set() |
| 222 | + _caches = deque(maxlen=cache_size) |
217 | 223 | log_method = getattr(logger, method) |
218 | 224 |
|
219 | 225 | def new_log_method(msg, *args, **kwargs): |
220 | 226 | key = f"{msg}:{args}" |
221 | 227 | if key not in _caches: |
222 | 228 | log_method(msg, *args, **kwargs) |
223 | | - _caches.add(key) |
| 229 | + _caches.append(key) |
224 | 230 |
|
225 | 231 | return new_log_method |
0 commit comments