logger.py/loghandler.py

43 lines
1007 B
Python
Raw Normal View History

2021-08-06 11:16:25 +02:00
import logging
import os
import sys
import colorlog
def loghandler():
debug_mode = os.getenv('DEBUG_MODE', False)
_logger = colorlog.getLogger(__name__)
if debug_mode:
_logger.setLevel(logging.DEBUG)
else:
_logger.setLevel(logging.INFO)
handler = colorlog.StreamHandler(sys.stdout)
if debug_mode:
handler.setLevel(logging.DEBUG)
else:
handler.setLevel(logging.INFO)
formatter = colorlog.ColoredFormatter(
'[%(asctime)s] %(log_color)s%(levelname)s%(reset)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S',
reset=True,
log_colors={
'DEBUG': 'cyan',
'INFO': 'green',
'WARNING': 'yellow',
'ERROR': 'red',
'CRITICAL': 'red,bg_white',
},
secondary_log_colors={},
style='%'
)
handler.setFormatter(formatter)
_logger.addHandler(handler)
return _logger
logger = loghandler()