apv

- 🐍 advanced python logging 📔
git clone git://git.acid.vegas/apv.git
Log | Files | Refs | Archive | README | LICENSE

console.py (2778B)

      1 import logging
      2 
      3 class LogColors:
      4     '''ANSI color codes for log messages.'''
      5     RESET     = '\033[0m'
      6     DATE      = '\033[90m'         # Dark Grey
      7     DEBUG     = '\033[96m'         # Cyan
      8     INFO      = '\033[92m'         # Green
      9     WARNING   = '\033[93m'         # Yellow
     10     ERROR     = '\033[91m'         # Red
     11     CRITICAL  = '\033[97m\033[41m' # White on Red
     12     FATAL     = '\033[97m\033[41m' # Same as CRITICAL
     13     NOTSET    = '\033[97m'         # White text
     14     SEPARATOR = '\033[90m'         # Dark Grey
     15     MODULE    = '\033[95m'         # Pink
     16     FUNCTION  = '\033[94m'         # Blue
     17     LINE      = '\033[33m'         # Orange
     18 
     19 class ColoredFormatter(logging.Formatter):
     20     def __init__(self, datefmt=None, show_details=False):
     21         super().__init__(datefmt=datefmt)
     22         self.show_details = show_details
     23         self.LEVEL_COLORS = {
     24             'NOTSET'   : LogColors.NOTSET,
     25             'DEBUG'    : LogColors.DEBUG,
     26             'INFO'     : LogColors.INFO,
     27             'WARNING'  : LogColors.WARNING,
     28             'ERROR'    : LogColors.ERROR,
     29             'CRITICAL' : LogColors.CRITICAL,
     30             'FATAL'    : LogColors.FATAL
     31         }
     32 
     33     def format(self, record):
     34         log_level = record.levelname
     35         message   = record.getMessage()
     36         asctime   = self.formatTime(record, self.datefmt)
     37         color     = self.LEVEL_COLORS.get(log_level, LogColors.RESET)
     38         separator = f'{LogColors.SEPARATOR} ┃ {LogColors.RESET}'
     39         
     40         if self.show_details:
     41             formatted = (
     42                 f'{LogColors.DATE}{asctime}{LogColors.RESET}'
     43                 f'{separator}'
     44                 f'{color}{log_level:<8}{LogColors.RESET}'
     45                 f'{separator}'
     46                 f'{LogColors.MODULE}{record.module}{LogColors.RESET}'
     47                 f'{separator}'
     48                 f'{LogColors.FUNCTION}{record.funcName}{LogColors.RESET}'
     49                 f'{separator}'
     50                 f'{LogColors.LINE}{record.lineno}{LogColors.RESET}'
     51                 f'{separator}'
     52                 f'{message}'
     53             )
     54         else:
     55             formatted = (
     56                 f'{LogColors.DATE}{asctime}{LogColors.RESET}'
     57                 f'{separator}'
     58                 f'{color}{log_level:<8}{LogColors.RESET}'
     59                 f'{separator}'
     60                 f'{message}'
     61             )
     62         return formatted
     63 
     64 def setup_console_handler(level_num: int, date_format: str, show_details: bool):
     65     '''Set up the console handler with colored output.'''
     66     console_handler = logging.StreamHandler()
     67     console_handler.setLevel(level_num)
     68     console_formatter = ColoredFormatter(datefmt=date_format, show_details=show_details)
     69     console_handler.setFormatter(console_formatter)
     70     logging.getLogger().addHandler(console_handler)