restic-compose-backup/restic_compose_backup/log.py

28 lines
856 B
Python
Raw Normal View History

2019-11-15 15:47:40 +00:00
import logging
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')
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"""
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)