From 818d18d4e0922ce70f5c0dd2797c9bfc38c3f926 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 9 Apr 2023 16:23:00 +0200 Subject: [PATCH] Add StdErrStreamHandler to logging --- freqtrade/loggers.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/freqtrade/loggers.py b/freqtrade/loggers.py index 823fa174e..a9ba7d7d8 100644 --- a/freqtrade/loggers.py +++ b/freqtrade/loggers.py @@ -1,12 +1,36 @@ import logging import sys -from logging import Formatter +from logging import Formatter, Handler from logging.handlers import BufferingHandler, RotatingFileHandler, SysLogHandler from freqtrade.constants import Config from freqtrade.exceptions import OperationalException +class FTStdErrStreamHandler(Handler): + def flush(self): + """ + Override Flush behaviour - we keep half of the configured capacity + otherwise, we have moments with "empty" logs. + """ + self.acquire() + try: + sys.stderr.flush() + finally: + self.release() + + def emit(self, record): + try: + msg = self.format(record) + # Don't keep a reference to stderr - this can be problematic with progressbars. + sys.stderr.write(msg + '\n') + self.flush() + except RecursionError: + raise + except Exception: + self.handleError(record) + + class FTBufferingHandler(BufferingHandler): def flush(self): """ @@ -69,7 +93,7 @@ def setup_logging_pre() -> None: logging.basicConfig( level=logging.INFO, format=LOGFORMAT, - handlers=[logging.StreamHandler(sys.stderr), bufferHandler] + handlers=[FTStdErrStreamHandler(), bufferHandler] )