2019-11-15 15:47:40 +00:00
|
|
|
import logging
|
2019-11-25 20:21:00 +00:00
|
|
|
import os
|
2019-11-15 15:47:40 +00:00
|
|
|
import sys
|
|
|
|
|
2019-12-03 08:40:02 +00:00
|
|
|
logger = logging.getLogger('restic_compose_backup')
|
2019-11-25 20:21:00 +00:00
|
|
|
HOSTNAME = os.environ['HOSTNAME']
|
2019-11-15 15:47:40 +00:00
|
|
|
|
2019-12-03 23:31:13 +00:00
|
|
|
DEFAULT_LOG_LEVEL = logging.INFO
|
|
|
|
LOG_LEVELS = {
|
|
|
|
'debug': logging.DEBUG,
|
|
|
|
'info': logging.INFO,
|
|
|
|
'warning': logging.WARNING,
|
|
|
|
'error': logging.ERROR,
|
|
|
|
}
|
|
|
|
|
|
|
|
def setup(level: str = 'warning'):
|
|
|
|
"""Set up logging"""
|
|
|
|
# Get log level from env if not set directly
|
|
|
|
if level is None:
|
|
|
|
level = os.environ.get('LOG_LEVEL')
|
|
|
|
|
|
|
|
level = level or ""
|
|
|
|
level = LOG_LEVELS.get(level.lower(), DEFAULT_LOG_LEVEL)
|
|
|
|
logger.setLevel(level)
|
|
|
|
|
|
|
|
ch = logging.StreamHandler(stream=sys.stdout)
|
|
|
|
ch.setLevel(level)
|
2019-12-04 00:30:05 +00:00
|
|
|
# ch.setFormatter(logging.Formatter(f'%(asctime)s - {HOSTNAME} - %(name)s - %(levelname)s - %(message)s'))
|
2019-12-04 02:11:46 +00:00
|
|
|
# ch.setFormatter(logging.Formatter(f'%(asctime)s - {HOSTNAME} - %(levelname)s - %(message)s'))
|
|
|
|
ch.setFormatter(logging.Formatter(f'%(asctime)s - %(levelname)s: %(message)s'))
|
2019-12-03 23:31:13 +00:00
|
|
|
logger.addHandler(ch)
|