mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-22 22:01:41 +03:00
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
import logging
|
|
import os
|
|
from logging.handlers import RotatingFileHandler
|
|
|
|
from colorlog import (
|
|
ColoredFormatter,
|
|
) # You need to install this package: pip install colorlog
|
|
|
|
|
|
def get_logger(logger_name, log_file="application.log"):
|
|
log_level = os.getenv("LOG_LEVEL", "WARNING").upper()
|
|
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="%",
|
|
)
|
|
|
|
console_handler = logging.StreamHandler()
|
|
console_handler.setFormatter(color_formatter)
|
|
|
|
file_handler = RotatingFileHandler(
|
|
log_file, maxBytes=5000000, backupCount=5
|
|
) # 5MB file
|
|
file_handler.setFormatter(formatter)
|
|
|
|
if not logger.handlers:
|
|
logger.addHandler(console_handler)
|
|
logger.addHandler(file_handler)
|
|
|
|
return logger
|