Skip to content

Commit

Permalink
Stop duping CustomLoggingFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rn3j committed Dec 17, 2024
1 parent afe4f86 commit ce916d3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 77 deletions.
33 changes: 1 addition & 32 deletions compile_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
46 changes: 2 additions & 44 deletions src/tauon/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
47 changes: 46 additions & 1 deletion src/tauon/t_modules/t_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down

0 comments on commit ce916d3

Please sign in to comment.