quivr/backend/logger.py

44 lines
1.2 KiB
Python
Raw Normal View History

2023-05-22 09:39:55 +03:00
import logging
from logging.handlers import RotatingFileHandler
2023-05-22 09:39:55 +03:00
from colorlog import (
ColoredFormatter,
) # You need to install this package: pip install colorlog
2023-05-22 09:39:55 +03:00
def get_logger(logger_name, log_level=logging.WARNING, log_file="application.log"):
2023-05-22 09:39:55 +03:00
logger = logging.getLogger(logger_name)
logger.setLevel(log_level)
logger.propagate = False # Prevent log propagation to avoid double logging
formatter = logging.Formatter(
"[%(levelname)s] %(name)s [%(filename)s:%(lineno)d]: %(message)s"
)
color_formatter = ColoredFormatter(
"%(log_color)s[%(levelname)s]%(reset)s %(name)s [%(filename)s:%(lineno)d]: %(message)s",
log_colors={
"DEBUG": "cyan",
"INFO": "green",
"WARNING": "yellow",
"ERROR": "red",
"CRITICAL": "red,bg_white",
},
reset=True,
style="%",
)
2023-05-22 09:39:55 +03:00
console_handler = logging.StreamHandler()
console_handler.setFormatter(color_formatter)
2023-05-22 09:39:55 +03:00
file_handler = RotatingFileHandler(
log_file, maxBytes=5000000, backupCount=5
) # 5MB file
file_handler.setFormatter(formatter)
2023-05-22 09:39:55 +03:00
if not logger.handlers:
logger.addHandler(console_handler)
logger.addHandler(file_handler)
2023-05-22 09:39:55 +03:00
return logger