freqtrade_origin/freqtrade/mixins/logging_mixin.py

39 lines
1.2 KiB
Python
Raw Normal View History

2020-11-22 10:49:41 +00:00
from typing import Callable
2020-12-07 14:45:02 +00:00
2020-10-13 05:11:08 +00:00
from cachetools import TTLCache, cached
class LoggingMixin():
"""
Logging Mixin
Shows similar messages only once every `refresh_period`.
"""
2020-11-19 19:06:13 +00:00
# Disable output completely
show_output = True
2020-10-13 05:11:08 +00:00
def __init__(self, logger, refresh_period: int = 3600):
"""
:param refresh_period: in seconds - Show identical messages in this intervals
"""
self.logger = logger
self.refresh_period = refresh_period
self._log_cache: TTLCache = TTLCache(maxsize=1024, ttl=self.refresh_period)
2020-11-22 10:49:41 +00:00
def log_once(self, message: str, logmethod: Callable) -> None:
2020-10-13 05:11:08 +00:00
"""
Logs message - not more often than "refresh_period" to avoid log spamming
Logs the log-message as debug as well to simplify debugging.
:param message: String containing the message to be sent to the function.
2020-11-22 10:49:41 +00:00
:param logmethod: Function that'll be called. Most likely `logger.info`.
2020-10-13 05:11:08 +00:00
:return: None.
"""
@cached(cache=self._log_cache)
2020-11-19 18:45:22 +00:00
def _log_once(message: str):
2020-10-13 05:11:08 +00:00
logmethod(message)
# Log as debug first
self.logger.debug(message)
# Call hidden function.
2020-11-19 19:06:13 +00:00
if self.show_output:
_log_once(message)