From 2c504d3123ad345c3bda0738ad1e889b6e1f2544 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Tue, 16 Jan 2024 10:37:22 +0100 Subject: [PATCH] fear: formatted logger --- src/music_kraken/__init__.py | 28 +++++++++++++++++++++++++- src/music_kraken/utils/enums/colors.py | 5 +++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/music_kraken/__init__.py b/src/music_kraken/__init__.py index 49c35c7..8eb5e9b 100644 --- a/src/music_kraken/__init__.py +++ b/src/music_kraken/__init__.py @@ -11,13 +11,39 @@ if True: import sys sys.setrecursionlimit(100) + +class CustomFormatter(logging.Formatter): + grey = "\x1b[38;20m" + yellow = "\x1b[33;20m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + reset = "\x1b[0m" + format = logging_settings['logging_format'] + + FORMATS = { + logging.DEBUG: grey + format + reset, + logging.INFO: grey + format + reset, + logging.WARNING: yellow + format + reset, + logging.ERROR: red + format + reset, + logging.CRITICAL: bold_red + format + reset + } + + def format(self, record): + log_fmt = self.FORMATS.get(record.levelno) + formatter = logging.Formatter(log_fmt) + return formatter.format(record) + + +stream_handler = logging.StreamHandler() +stream_handler.setFormatter(CustomFormatter()) + # configure logger default logging.basicConfig( level=logging_settings['log_level'] if not DEBUG_LOGGIN else logging.DEBUG, format=logging_settings['logging_format'], handlers=[ logging.FileHandler(main_settings['log_file']), - logging.StreamHandler() + stream_handler ] ) diff --git a/src/music_kraken/utils/enums/colors.py b/src/music_kraken/utils/enums/colors.py index 6c23cc1..44f79da 100644 --- a/src/music_kraken/utils/enums/colors.py +++ b/src/music_kraken/utils/enums/colors.py @@ -12,3 +12,8 @@ class BColors: ENDC = "\033[0m" BOLD = "\033[1m" UNDERLINE = "\033[4m" + + GREY = "\x1b[38;20m" + YELLOW = "\x1b[33;20m" + RED = "\x1b[31;20m" + BOLD_RED = "\x1b[31;1m"