2019-07-06 22:40:52 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
2019-10-25 06:50:37 +00:00
|
|
|
from logging import Formatter
|
|
|
|
from logging.handlers import RotatingFileHandler, SysLogHandler
|
2019-07-06 22:40:52 +00:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
2019-12-30 14:02:17 +00:00
|
|
|
from freqtrade.exceptions import OperationalException
|
2019-10-25 06:50:37 +00:00
|
|
|
|
2019-07-06 22:40:52 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-05-31 07:51:45 +00:00
|
|
|
def _set_loggers(verbosity: int = 0, api_verbosity: str = 'info') -> None:
|
2019-07-06 22:40:52 +00:00
|
|
|
"""
|
2019-07-07 07:17:01 +00:00
|
|
|
Set the logging level for third party libraries
|
2019-07-06 22:40:52 +00:00
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
|
|
|
|
logging.getLogger('requests').setLevel(
|
2020-03-10 19:30:36 +00:00
|
|
|
logging.INFO if verbosity <= 1 else logging.DEBUG
|
2019-07-06 22:40:52 +00:00
|
|
|
)
|
|
|
|
logging.getLogger("urllib3").setLevel(
|
2020-03-10 19:30:36 +00:00
|
|
|
logging.INFO if verbosity <= 1 else logging.DEBUG
|
2019-07-06 22:40:52 +00:00
|
|
|
)
|
|
|
|
logging.getLogger('ccxt.base.exchange').setLevel(
|
2020-03-10 19:30:36 +00:00
|
|
|
logging.INFO if verbosity <= 2 else logging.DEBUG
|
2019-07-06 22:40:52 +00:00
|
|
|
)
|
|
|
|
logging.getLogger('telegram').setLevel(logging.INFO)
|
|
|
|
|
2020-05-31 07:51:45 +00:00
|
|
|
logging.getLogger('werkzeug').setLevel(
|
|
|
|
logging.ERROR if api_verbosity == 'error' else logging.INFO
|
|
|
|
)
|
|
|
|
|
2019-07-06 22:40:52 +00:00
|
|
|
|
|
|
|
def setup_logging(config: Dict[str, Any]) -> None:
|
|
|
|
"""
|
2019-07-06 23:53:13 +00:00
|
|
|
Process -v/--verbose, --logfile options
|
2019-07-06 22:40:52 +00:00
|
|
|
"""
|
|
|
|
# Log level
|
|
|
|
verbosity = config['verbosity']
|
|
|
|
|
2019-10-30 01:04:28 +00:00
|
|
|
# Log to stderr
|
|
|
|
log_handlers: List[logging.Handler] = [logging.StreamHandler(sys.stderr)]
|
2019-07-06 22:40:52 +00:00
|
|
|
|
2019-10-25 06:50:37 +00:00
|
|
|
logfile = config.get('logfile')
|
|
|
|
if logfile:
|
|
|
|
s = logfile.split(':')
|
|
|
|
if s[0] == 'syslog':
|
|
|
|
# Address can be either a string (socket filename) for Unix domain socket or
|
|
|
|
# a tuple (hostname, port) for UDP socket.
|
|
|
|
# Address can be omitted (i.e. simple 'syslog' used as the value of
|
|
|
|
# config['logfilename']), which defaults to '/dev/log', applicable for most
|
|
|
|
# of the systems.
|
|
|
|
address = (s[1], int(s[2])) if len(s) > 2 else s[1] if len(s) > 1 else '/dev/log'
|
|
|
|
handler = SysLogHandler(address=address)
|
|
|
|
# No datetime field for logging into syslog, to allow syslog
|
|
|
|
# to perform reduction of repeating messages if this is set in the
|
|
|
|
# syslog config. The messages should be equal for this.
|
|
|
|
handler.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s'))
|
|
|
|
log_handlers.append(handler)
|
|
|
|
elif s[0] == 'journald':
|
|
|
|
try:
|
|
|
|
from systemd.journal import JournaldLogHandler
|
|
|
|
except ImportError:
|
|
|
|
raise OperationalException("You need the systemd python package be installed in "
|
|
|
|
"order to use logging to journald.")
|
|
|
|
handler = JournaldLogHandler()
|
|
|
|
# No datetime field for logging into journald, to allow syslog
|
|
|
|
# to perform reduction of repeating messages if this is set in the
|
|
|
|
# syslog config. The messages should be equal for this.
|
|
|
|
handler.setFormatter(Formatter('%(name)s - %(levelname)s - %(message)s'))
|
|
|
|
log_handlers.append(handler)
|
|
|
|
else:
|
|
|
|
log_handlers.append(RotatingFileHandler(logfile,
|
|
|
|
maxBytes=1024 * 1024, # 1Mb
|
|
|
|
backupCount=10))
|
2019-07-06 22:40:52 +00:00
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO if verbosity < 1 else logging.DEBUG,
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
handlers=log_handlers
|
|
|
|
)
|
2020-05-31 07:51:45 +00:00
|
|
|
_set_loggers(verbosity, config.get('api_server', {}).get('verbosity', 'info'))
|
2019-07-06 22:40:52 +00:00
|
|
|
logger.info('Verbosity set to %s', verbosity)
|