Commit fb4f7873 authored by Arnaud ROUX's avatar Arnaud ROUX

torebase

parent e5bde3bf
Pipeline #3 failed with stages
in 1 minute and 19 seconds
......@@ -5,43 +5,42 @@ import os
from django.conf import settings
def get_logger(log_name, logfile_handler_level=logging.INFO, console_handler_level_on_production=logging.CRITICAL)\
-> logging.Logger:
"""Return logger object which own two handlers at differents log level.
logfile_handler_level -- define TimedRotatingFileHandler log level. (default: logging.INFO)
console_handler_level_on_production -- define StreamHandler for console output, *always* DEBUG if django
settings on `DEBUG=True`. (default: logging.CRITICAL)
"""
def get_logger(log_name, create_console_handler=False) -> logging.Logger:
"""Return logger object which own two handlers at differents log level."""
os.makedirs(settings.LOG_DIR, exist_ok=True)
logger = logging.getLogger(log_name)
logfile_handler = logging.handlers.TimedRotatingFileHandler(os.path.join(settings.LOG_DIR, log_name),
when="midnight",
interval=1,
backupCount=30)
logfile_formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(message)s', '%Y-%m-%d %H:%M:%S')
logfile_handler.setLevel(logfile_handler_level)
logfile_handler.setLevel(logging.INFO)
logfile_handler.setFormatter(logfile_formatter)
logfile_handler.suffix = "%Y-%m-%d"
console_handler = logging.StreamHandler()
if settings.DEBUG:
console_handler.setLevel(logging.DEBUG)
else:
console_handler.setLevel(console_handler_level_on_production)
logger = logging.getLogger(log_name)
console_formatter = logging.Formatter('[%(levelname)s] %(message)s')
console_handler.setFormatter(console_formatter)
# logfile_handler is added only if they are no handler with they are not another instance with same type and file target.
same_rotating_file_handler_exist = any([ isinstance(handler, logging.handlers.TimedRotatingFileHandler)
for handler in logger.handlers
if getattr(handler, 'baseFilename', '') == logfile_handler.baseFilename])
if not logger.handlers:
if not same_rotating_file_handler_exist:
logger.addHandler(logfile_handler)
# Console handler added only if they are no console handlers ever attached to 'logger'
if not any((isinstance(handler, logging.StreamHandler) for handler in logger.handlers)):
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.CRITICAL)
if settings.DEBUG:
console_handler.setLevel(logging.DEBUG)
console_formatter = logging.Formatter('[%(levelname)s] %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)
# Only handlers define log level, logger always on debug
logger.setLevel(logging.DEBUG)
return logger
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment