Spaces:
Sleeping
Sleeping
File size: 1,964 Bytes
e947fcb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
"""Module doc string"""
import logging
import os
import sys
from colorama import Back, Fore, Style, init
LOGGER_LEVEL = os.getenv("LOGGER_LEVEL", "INFO")
# Initialize colorama
init(autoreset=True)
logger = logging.getLogger(__name__)
if not logger.hasHandlers():
logger.propagate = False
logger.setLevel(LOGGER_LEVEL)
# Define color codes for different log levels
log_colors = {
logging.DEBUG: Fore.CYAN,
logging.INFO: Fore.GREEN,
logging.WARNING: Fore.YELLOW,
logging.ERROR: Fore.RED,
logging.CRITICAL: Fore.RED + Back.WHITE + Style.BRIGHT,
}
class ColoredFormatter(logging.Formatter):
"""Module doc string"""
def format(self, record):
"""Module doc string"""
levelno = record.levelno
color = log_colors.get(levelno, "")
# Format the message
message = record.getMessage()
# Format the rest of the log details
details = self._fmt % {
"asctime": self.formatTime(record, self.datefmt),
"levelname": record.levelname,
"module": record.module,
"funcName": record.funcName,
"lineno": record.lineno,
}
# Combine details and colored message
return f"{Fore.WHITE}{details} :: {color}{message}{Style.RESET_ALL}"
normal_handler = logging.StreamHandler(sys.stdout)
normal_handler.setLevel(logging.DEBUG)
normal_handler.addFilter(lambda logRecord: logRecord.levelno < logging.WARNING)
error_handler = logging.StreamHandler(sys.stderr)
error_handler.setLevel(logging.WARNING)
formatter = ColoredFormatter(
"%(asctime)s :: %(levelname)s :: %(module)s :: %(funcName)s :: %(lineno)d"
)
normal_handler.setFormatter(formatter)
error_handler.setFormatter(formatter)
logger.addHandler(normal_handler)
logger.addHandler(error_handler)
|