Add StdErrStreamHandler to logging

This commit is contained in:
Matthias 2023-04-09 16:23:00 +02:00
parent b6aac5079b
commit 818d18d4e0

View File

@ -1,12 +1,36 @@
import logging import logging
import sys import sys
from logging import Formatter from logging import Formatter, Handler
from logging.handlers import BufferingHandler, RotatingFileHandler, SysLogHandler from logging.handlers import BufferingHandler, RotatingFileHandler, SysLogHandler
from freqtrade.constants import Config from freqtrade.constants import Config
from freqtrade.exceptions import OperationalException 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): class FTBufferingHandler(BufferingHandler):
def flush(self): def flush(self):
""" """
@ -69,7 +93,7 @@ def setup_logging_pre() -> None:
logging.basicConfig( logging.basicConfig(
level=logging.INFO, level=logging.INFO,
format=LOGFORMAT, format=LOGFORMAT,
handlers=[logging.StreamHandler(sys.stderr), bufferHandler] handlers=[FTStdErrStreamHandler(), bufferHandler]
) )