Refactor logging to be a package, instead of a module
This commit is contained in:
parent
818d18d4e0
commit
ed57e7d43b
@ -1,48 +1,11 @@
|
|||||||
import logging
|
import logging
|
||||||
import sys
|
from logging import Formatter
|
||||||
from logging import Formatter, Handler
|
from logging.handlers import 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
|
||||||
|
from freqtrade.loggers.buffering_handler import FTBufferingHandler
|
||||||
|
from freqtrade.loggers.std_err_stream_handler import FTStdErrStreamHandler
|
||||||
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):
|
|
||||||
"""
|
|
||||||
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__)
|
logger = logging.getLogger(__name__)
|
15
freqtrade/loggers/buffering_handler.py
Normal file
15
freqtrade/loggers/buffering_handler.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
from logging.handlers import BufferingHandler
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
26
freqtrade/loggers/std_err_stream_handler.py
Normal file
26
freqtrade/loggers/std_err_stream_handler.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import sys
|
||||||
|
from logging import Handler
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
Loading…
Reference in New Issue
Block a user