From ce916d3a709edec04cd59e2d20ddb69c84924005 Mon Sep 17 00:00:00 2001 From: Martin Rys Date: Tue, 17 Dec 2024 18:25:11 +0100 Subject: [PATCH] Stop duping CustomLoggingFormatter --- compile_translations.py | 33 +----------------------- src/tauon/__main__.py | 46 ++------------------------------- src/tauon/t_modules/t_extra.py | 47 +++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 77 deletions(-) diff --git a/compile_translations.py b/compile_translations.py index 102d6f2bff..f66e36c95a 100644 --- a/compile_translations.py +++ b/compile_translations.py @@ -3,38 +3,7 @@ import subprocess from pathlib import Path - -# TODO(Martin): import this class from tauon.py instead -class CustomLoggingFormatter(logging.Formatter): - """Nicely format logging.loglevel logs""" - - grey = "\x1b[38;20m" - grey_bold = "\x1b[38;1m" - yellow = "\x1b[33;20m" - yellow_bold = "\x1b[33;1m" - red = "\x1b[31;20m" - bold_red = "\x1b[31;1m" - reset = "\x1b[0m" - format = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s" - format_verbose = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s (%(filename)s:%(lineno)d)" - - FORMATS = { - logging.DEBUG: grey_bold + format_verbose + reset, - logging.INFO: yellow + format + reset, - logging.WARNING: yellow_bold + format + reset, - logging.ERROR: red + format + reset, - logging.CRITICAL: bold_red + format_verbose + reset, - } - - def format(self, record: dict) -> str: - log_fmt = self.FORMATS.get(record.levelno) - # Remove the miliseconds(%f) from the default string - date_fmt = "%Y-%m-%d %H:%M:%S" - formatter = logging.Formatter(log_fmt, date_fmt) - # Center align + min length things to prevent logs jumping around when switching between different values - record.levelname = f"{record.levelname:^7}" - record.module = f"{record.module:^10}" - return formatter.format(record) +from src.tauon.t_modules.t_extra import CustomLoggingFormatter # DEBUG+ to file and std_err logging.basicConfig( diff --git a/src/tauon/__main__.py b/src/tauon/__main__.py index f026bb5bb9..b95ecabee1 100755 --- a/src/tauon/__main__.py +++ b/src/tauon/__main__.py @@ -62,55 +62,13 @@ ) from sdl2.sdlimage import IMG_Load +from tauon.t_modules.t_extra import CustomLoggingFormatter, LogHistoryHandler + install_directory: Path = Path(__file__).resolve().parent sys.path.append(str(install_directory.parent)) from tauon.t_modules import t_bootstrap - -class CustomLoggingFormatter(logging.Formatter): - """Nicely format logging.loglevel logs""" - - grey = "\x1b[0;20m" - grey_bold = "\x1b[0;1m" - yellow = "\x1b[33;20m" - yellow_bold = "\x1b[33;1m" - red = "\x1b[31;20m" - bold_red = "\x1b[31;1m" - purple = "\x1b[0;35m" - reset = "\x1b[0m" - format = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s" - format_verbose = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s (%(filename)s:%(lineno)d)" - - # TODO(Martin): Add some way in which devel mode uses everything verbose - FORMATS = { - logging.DEBUG: grey_bold + format_verbose + reset, - logging.INFO: grey + format + reset, - logging.WARNING: purple + format_verbose + reset, - logging.ERROR: red + format_verbose + reset, - logging.CRITICAL: bold_red + format_verbose + reset, - } - - def format(self, record: dict) -> str: - log_fmt = self.FORMATS.get(record.levelno) - # Remove the miliseconds(%f) from the default string - date_fmt = "%Y-%m-%d %H:%M:%S" - formatter = logging.Formatter(log_fmt, date_fmt) - # Center align + min length things to prevent logs jumping around when switching between different values - record.levelname = f"{record.levelname:^7}" - record.module = f"{record.module:^10}" - return formatter.format(record) - -class LogHistoryHandler(logging.Handler): - def __init__(self): - super().__init__() - self.log_history = [] # List to store log messages - - def emit(self, record: dict): - self.log_history.append(record) # Append to the log history - if len(self.log_history) > 50: - del self.log_history[0] - log = LogHistoryHandler() formatter = logging.Formatter('[%(levelname)s] %(message)s') log.setFormatter(formatter) diff --git a/src/tauon/t_modules/t_extra.py b/src/tauon/t_modules/t_extra.py index 35bd2f3cab..b501d8c746 100644 --- a/src/tauon/t_modules/t_extra.py +++ b/src/tauon/t_modules/t_extra.py @@ -36,7 +36,8 @@ import urllib.parse import zipfile from dataclasses import dataclass -from typing import TYPE_CHECKING +from logging import LogRecord +from typing import TYPE_CHECKING, override from gi.repository import GLib @@ -45,6 +46,50 @@ from tauon.t_modules.t_main import TrackClass +class CustomLoggingFormatter(logging.Formatter): + """Nicely format logging.loglevel logs""" + + grey = "\x1b[0;20m" + grey_bold = "\x1b[0;1m" + yellow = "\x1b[33;20m" + yellow_bold = "\x1b[33;1m" + red = "\x1b[31;20m" + bold_red = "\x1b[31;1m" + purple = "\x1b[0;35m" + reset = "\x1b[0m" + format_simple = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s" + format_verbose = "%(asctime)s [%(levelname)s] [%(module)s] %(message)s (%(filename)s:%(lineno)d)" + + # TODO(Martin): Add some way in which devel mode uses everything verbose + FORMATS = { + logging.DEBUG: grey_bold + format_verbose + reset, + logging.INFO: grey + format_simple + reset, + logging.WARNING: purple + format_verbose + reset, + logging.ERROR: red + format_verbose + reset, + logging.CRITICAL: bold_red + format_verbose + reset, + } + + @override + def format(self, record: LogRecord) -> str: + log_fmt = self.FORMATS.get(record.levelno) + # Remove the miliseconds(%f) from the default string + date_fmt = "%Y-%m-%d %H:%M:%S" + formatter = logging.Formatter(log_fmt, date_fmt) + # Center align + min length things to prevent logs jumping around when switching between different values + record.levelname = f"{record.levelname:^7}" + record.module = f"{record.module:^10}" + return formatter.format(record) + +class LogHistoryHandler(logging.Handler): + def __init__(self): + super().__init__() + self.log_history = [] # List to store log messages + + @override + def emit(self, record: LogRecord): + self.log_history.append(record) # Append to the log history + if len(self.log_history) > 50: + del self.log_history[0] @dataclass class TauonQueueItem: