Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuhtz committed Jul 16, 2024
1 parent abc9213 commit 6877e17
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions api/tooling/logger_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import sys

class CustomFormatter(logging.Formatter):
"""
logging.Formatter to modify the logging format based on logging level
"""
def __init__(self, fmt=None, datefmt=None, style='%', level_formats=None):
super().__init__(fmt, datefmt, style)
self.level_formats = level_formats or {}
Expand All @@ -13,22 +16,25 @@ def format(self, record):
self._style._fmt = self.level_formats[record.levelno]
return super().format(record)

# https://stackoverflow.com/questions/384076/how-can-i-color-python-logging-output
green = '\033[32m'
blue = '\033[34m'
white = '\033[37m'
yellow = '\033[33m'
red = '\033[31m'
bold_red = '\033[31;1m'
reset = '\033[0m'
class ANSIColors:
"""ANSI escape sequences for colors to use in console text strings
"""
green = '\033[32m'
blue = '\033[34m'
white = '\033[37m'
yellow = '\033[33m'
red = '\033[31m'
bold_red = '\033[31;1m'
cyan = '\033[47m'
reset = '\033[0m'

# Define custom formats for each severity level
level_formats = {
logging.DEBUG: green + '%(levelname)s:' + white + '\t %(asctime)s %(module)s' + reset + ' %(message)s',
logging.INFO: blue + '%(levelname)s:' + reset + '\t %(message)s',
logging.WARNING: yellow + '%(levelname)s:' + reset + '\t %(message)s',
logging.ERROR: red + '%(levelname)s:' + reset + '\t %(message)s',
logging.CRITICAL: bold_red + '%(levelname)s:' + reset + '\t %(message)s'
logging.DEBUG: ANSIColors.green + '%(levelname)s:' + ANSIColors.white + '\t %(asctime)s %(module)s' + ANSIColors.reset + ' %(message)s',
logging.INFO: ANSIColors.cyan + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s',
logging.WARNING: ANSIColors.yellow + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s',
logging.ERROR: ANSIColors.red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s',
logging.CRITICAL: ANSIColors.bold_red + '%(levelname)s:' + ANSIColors.reset + '\t %(message)s'
}

def logger_init(startup_logging_level: str = 'INFO') -> logging.Logger:
Expand Down

0 comments on commit 6877e17

Please sign in to comment.