From 31448bd7da9843d70a61293154aed5b928b4c114 Mon Sep 17 00:00:00 2001 From: Yozora Date: Mon, 23 Dec 2024 16:51:03 +0000 Subject: [PATCH] Rename logs to `kometa.log`, `kometa-1.log`, `kometa-2.log` etc --- CHANGELOG | 1 + modules/logs.py | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 65b8c56e..84a58c98 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -39,3 +39,4 @@ Removed Blog from the Navigation due to lack of time for updating/maintaining it Fixes #2354 by updating version of tmdbapi dependency Added Start Time, Finished and Run Time to Summary of run. Fixed an issue where custom repositories would not work correctly if the URL did not end in a trailing `/` character. +Log files will now follow the naming convention of `kometa.log`, `kometa-1.log` (previous run), `kometa-2.log (2 runs ago) etc. diff --git a/modules/logs.py b/modules/logs.py index e548cb8a..d0d70b4e 100644 --- a/modules/logs.py +++ b/modules/logs.py @@ -4,7 +4,7 @@ from logging.handlers import RotatingFileHandler LOG_DIR = "logs" COLLECTION_DIR = "collections" PLAYLIST_DIR = "playlists" -MAIN_LOG = "meta.log" +MAIN_LOG = "kometa.log" LIBRARY_LOG = "library.log" COLLECTION_LOG = "collection.log" PLAYLIST_LOG = "playlist.log" @@ -27,6 +27,35 @@ def fmt_filter(record): _srcfile = os.path.normcase(fmt_filter.__code__.co_filename) +import os +from logging.handlers import RotatingFileHandler + +class CustomRotatingFileHandler(RotatingFileHandler): + def rotation_filename(self, default_name): + dirname, basename = os.path.split(default_name) + base, ext = os.path.splitext(basename) + base = base.split('-')[0] if '-' in base else base + return os.path.join(dirname, f"{base}-{self.backupCount}{ext}") + + def doRollover(self): + if self.stream: + self.stream.close() + self.stream = None + + base_name = self.baseFilename.rsplit('.', 1)[0] + for i in range(self.backupCount - 1, 0, -1): + source = f"{base_name}-{i}.log" + dest = f"{base_name}-{i + 1}.log" + if os.path.exists(source): + os.replace(source, dest) + + dest = f"{base_name}-1.log" + if os.path.exists(self.baseFilename): + os.replace(self.baseFilename, dest) + + self.stream = self._open() + + class MyLogger: def __init__(self, logger_name, default_dir, screen_width, separating_character, ignore_ghost, is_debug, is_trace, log_requests): @@ -63,16 +92,14 @@ class MyLogger: def clear_errors(self): self.saved_errors = [] - def _get_handler(self, log_file, count=3): - _handler = RotatingFileHandler(log_file, delay=True, mode="w", backupCount=count, encoding="utf-8") + _handler = CustomRotatingFileHandler(log_file, delay=True, mode="w", backupCount=count, encoding="utf-8") self._formatter(handler=_handler) if os.path.isfile(log_file): self._logger.removeHandler(_handler) _handler.doRollover() self._logger.addHandler(_handler) return _handler - def _formatter(self, handler=None, border=True, trace=False, log_only=False, space=False): console = f"| %(message)-{self.screen_width - 2}s |" if border else f"%(message)-{self.screen_width - 2}s" file = f"{' '*65}" if space else f"[%(asctime)s] %(filename)-27s {'[TRACE] ' if trace else '%(levelname)-10s'} "