From d08885ed92f390c5ded5799b243a5ba5f18d92e0 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 16 Jan 2022 15:37:00 +0100 Subject: [PATCH] Fix empty "/log" endpoint in certain moments --- freqtrade/loggers.py | 16 +++++++++++++++- tests/test_configuration.py | 6 +++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/freqtrade/loggers.py b/freqtrade/loggers.py index 5c5831695..e5b6ddbe9 100644 --- a/freqtrade/loggers.py +++ b/freqtrade/loggers.py @@ -7,11 +7,25 @@ from typing import Any, Dict from freqtrade.exceptions import OperationalException +class FTBufferingHandler(BufferingHandler): + def flush(self): + """ + Override Flush behaviour - we keep half of the configured capacity + otherwise, we have moments with "empty" logs. + """ + self.acquire() + try: + # Keep half of the records in buffer. + self.buffer = self.buffer[-int(self.capacity / 2):] + finally: + self.release() + + logger = logging.getLogger(__name__) LOGFORMAT = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' # Initialize bufferhandler - will be used for /log endpoints -bufferHandler = BufferingHandler(1000) +bufferHandler = FTBufferingHandler(1000) bufferHandler.setFormatter(Formatter(LOGFORMAT)) diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 57add66bf..0a6935649 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -22,7 +22,7 @@ from freqtrade.configuration.load_config import load_config_file, load_file, log from freqtrade.constants import DEFAULT_DB_DRYRUN_URL, DEFAULT_DB_PROD_URL, ENV_VAR_PREFIX from freqtrade.enums import RunMode from freqtrade.exceptions import OperationalException -from freqtrade.loggers import _set_loggers, setup_logging, setup_logging_pre +from freqtrade.loggers import FTBufferingHandler, _set_loggers, setup_logging, setup_logging_pre from tests.conftest import log_has, log_has_re, patched_configuration_load_config_file @@ -686,7 +686,7 @@ def test_set_loggers_syslog(): assert len(logger.handlers) == 3 assert [x for x in logger.handlers if type(x) == logging.handlers.SysLogHandler] assert [x for x in logger.handlers if type(x) == logging.StreamHandler] - assert [x for x in logger.handlers if type(x) == logging.handlers.BufferingHandler] + assert [x for x in logger.handlers if type(x) == FTBufferingHandler] # setting up logging again should NOT cause the loggers to be added a second time. setup_logging(config) assert len(logger.handlers) == 3 @@ -709,7 +709,7 @@ def test_set_loggers_Filehandler(tmpdir): assert len(logger.handlers) == 3 assert [x for x in logger.handlers if type(x) == logging.handlers.RotatingFileHandler] assert [x for x in logger.handlers if type(x) == logging.StreamHandler] - assert [x for x in logger.handlers if type(x) == logging.handlers.BufferingHandler] + assert [x for x in logger.handlers if type(x) == FTBufferingHandler] # setting up logging again should NOT cause the loggers to be added a second time. setup_logging(config) assert len(logger.handlers) == 3