2019-07-06 22:40:52 +00:00
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def _set_loggers(verbosity: int = 0) -> None:
|
|
|
|
"""
|
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(
|
|
|
|
logging.INFO if verbosity <= 1 else logging.DEBUG
|
|
|
|
)
|
|
|
|
logging.getLogger("urllib3").setLevel(
|
|
|
|
logging.INFO if verbosity <= 1 else logging.DEBUG
|
|
|
|
)
|
|
|
|
logging.getLogger('ccxt.base.exchange').setLevel(
|
|
|
|
logging.INFO if verbosity <= 2 else logging.DEBUG
|
|
|
|
)
|
|
|
|
logging.getLogger('telegram').setLevel(logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
if config.get('logfile'):
|
|
|
|
log_handlers.append(RotatingFileHandler(config['logfile'],
|
|
|
|
maxBytes=1024 * 1024, # 1Mb
|
|
|
|
backupCount=10))
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO if verbosity < 1 else logging.DEBUG,
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
handlers=log_handlers
|
|
|
|
)
|
|
|
|
_set_loggers(verbosity)
|
|
|
|
logger.info('Verbosity set to %s', verbosity)
|