refact: minor cleanup

This commit is contained in:
sebthom
2025-02-10 22:06:03 +01:00
parent 5ade82b54d
commit f3beb795b4
3 changed files with 25 additions and 29 deletions

View File

@@ -3,7 +3,7 @@ SPDX-FileCopyrightText: © Jens Bergman and contributors
SPDX-License-Identifier: AGPL-3.0-or-later SPDX-License-Identifier: AGPL-3.0-or-later
SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/ SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
""" """
import json, os, hashlib import hashlib, json, os
from typing import Any from typing import Any

View File

@@ -8,8 +8,7 @@ from collections.abc import Sized
from typing import Any, Final, NamedTuple from typing import Any, Final, NamedTuple
from kleinanzeigen_bot import resources from kleinanzeigen_bot import resources
from . import reflect from . import dicts, reflect
from . import dicts
__all__ = [ __all__ = [
"Locale", "Locale",

View File

@@ -5,6 +5,7 @@ SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanze
""" """
import copy, logging, re, sys import copy, logging, re, sys
from gettext import gettext as _ from gettext import gettext as _
from logging import Logger, DEBUG, INFO, WARNING, ERROR, CRITICAL
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
from typing import Any, Final # @UnusedImport from typing import Any, Final # @UnusedImport
@@ -23,10 +24,6 @@ __all__ = [
"is_debug" "is_debug"
] ]
Logger = logging.Logger
DEBUG:Final[int] = logging.DEBUG
INFO:Final[int] = logging.INFO
LOG_ROOT:Final[logging.Logger] = logging.getLogger() LOG_ROOT:Final[logging.Logger] = logging.getLogger()
@@ -34,25 +31,25 @@ def configure_console_logging() -> None:
class CustomFormatter(logging.Formatter): class CustomFormatter(logging.Formatter):
LEVEL_COLORS = { LEVEL_COLORS = {
logging.DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT, DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT,
logging.INFO: colorama.Fore.BLACK + colorama.Style.BRIGHT, INFO: colorama.Fore.BLACK + colorama.Style.BRIGHT,
logging.WARNING: colorama.Fore.YELLOW, WARNING: colorama.Fore.YELLOW,
logging.ERROR: colorama.Fore.RED, ERROR: colorama.Fore.RED,
logging.CRITICAL: colorama.Fore.RED, CRITICAL: colorama.Fore.RED,
} }
MESSAGE_COLORS = { MESSAGE_COLORS = {
logging.DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT, DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT,
logging.INFO: colorama.Fore.RESET, INFO: colorama.Fore.RESET,
logging.WARNING: colorama.Fore.YELLOW, WARNING: colorama.Fore.YELLOW,
logging.ERROR: colorama.Fore.RED, ERROR: colorama.Fore.RED,
logging.CRITICAL: colorama.Fore.RED + colorama.Style.BRIGHT, CRITICAL: colorama.Fore.RED + colorama.Style.BRIGHT,
} }
VALUE_COLORS = { VALUE_COLORS = {
logging.DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT, DEBUG: colorama.Fore.BLACK + colorama.Style.BRIGHT,
logging.INFO: colorama.Fore.MAGENTA, INFO: colorama.Fore.MAGENTA,
logging.WARNING: colorama.Fore.MAGENTA, WARNING: colorama.Fore.MAGENTA,
logging.ERROR: colorama.Fore.MAGENTA, ERROR: colorama.Fore.MAGENTA,
logging.CRITICAL: colorama.Fore.MAGENTA, CRITICAL: colorama.Fore.MAGENTA,
} }
def format(self, record:logging.LogRecord) -> str: def format(self, record:logging.LogRecord) -> str:
@@ -63,7 +60,7 @@ def configure_console_logging() -> None:
value_color = self.VALUE_COLORS.get(record.levelno, "") value_color = self.VALUE_COLORS.get(record.levelno, "")
# translate and colorize log level name # translate and colorize log level name
levelname = _(record.levelname) if record.levelno > logging.DEBUG else record.levelname levelname = _(record.levelname) if record.levelno > DEBUG else record.levelname
record.levelname = f"{level_color}[{levelname}]{colorama.Style.RESET_ALL}" record.levelname = f"{level_color}[{levelname}]{colorama.Style.RESET_ALL}"
# highlight message values enclosed by [...], "...", and '...' # highlight message values enclosed by [...], "...", and '...'
@@ -81,15 +78,15 @@ def configure_console_logging() -> None:
formatter = CustomFormatter("%(levelname)s %(message)s") formatter = CustomFormatter("%(levelname)s %(message)s")
stdout_log = logging.StreamHandler(sys.stderr) stdout_log = logging.StreamHandler(sys.stderr)
stdout_log.setLevel(logging.DEBUG) stdout_log.setLevel(DEBUG)
stdout_log.addFilter(type("", (logging.Filter,), { stdout_log.addFilter(type("", (logging.Filter,), {
"filter": lambda rec: rec.levelno <= logging.INFO "filter": lambda rec: rec.levelno <= INFO
})) }))
stdout_log.setFormatter(formatter) stdout_log.setFormatter(formatter)
LOG_ROOT.addHandler(stdout_log) LOG_ROOT.addHandler(stdout_log)
stderr_log = logging.StreamHandler(sys.stderr) stderr_log = logging.StreamHandler(sys.stderr)
stderr_log.setLevel(logging.WARNING) stderr_log.setLevel(WARNING)
stderr_log.setFormatter(formatter) stderr_log.setFormatter(formatter)
LOG_ROOT.addHandler(stderr_log) LOG_ROOT.addHandler(stderr_log)
@@ -128,7 +125,7 @@ def configure_file_logging(log_file_path:str) -> LogFileHandle:
backupCount = 10, backupCount = 10,
encoding = "utf-8" encoding = "utf-8"
) )
fh.setLevel(logging.DEBUG) fh.setLevel(DEBUG)
fh.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")) fh.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s"))
LOG_ROOT.addHandler(fh) LOG_ROOT.addHandler(fh)
return LogFileHandle(log_file_path, fh, LOG_ROOT) return LogFileHandle(log_file_path, fh, LOG_ROOT)
@@ -147,7 +144,7 @@ def get_logger(name: str | None = None) -> logging.Logger:
class TranslatingLogger(logging.Logger): class TranslatingLogger(logging.Logger):
def _log(self, level: int, msg: object, *args: Any, **kwargs: Any) -> None: def _log(self, level: int, msg: object, *args: Any, **kwargs: Any) -> None:
if level != logging.DEBUG: # debug messages should not be translated if level != DEBUG: # debug messages should not be translated
msg = i18n.translate(msg, reflect.get_caller(2)) msg = i18n.translate(msg, reflect.get_caller(2))
super()._log(level, msg, *args, **kwargs) super()._log(level, msg, *args, **kwargs)
@@ -156,4 +153,4 @@ def get_logger(name: str | None = None) -> logging.Logger:
def is_debug(logger:Logger) -> bool: def is_debug(logger:Logger) -> bool:
return logger.isEnabledFor(logging.DEBUG) return logger.isEnabledFor(DEBUG)