diff --git a/ToDo.md b/ToDo.md index ee86940..4e4dd60 100644 --- a/ToDo.md +++ b/ToDo.md @@ -3,3 +3,5 @@ - [x] stub files .pyi - [ ] Add tests for all, we need 100% test coverate - [x] Log: add custom format for "stack_correct" if set, this will override the normal stack block +- [ ] Log: add rotate for size based +- [ ] All folders and file names need to be revisited for naming and content collection diff --git a/src/corelibs/logging_handling/log.py b/src/corelibs/logging_handling/log.py index 03af8b3..1ed69aa 100644 --- a/src/corelibs/logging_handling/log.py +++ b/src/corelibs/logging_handling/log.py @@ -7,6 +7,7 @@ attach "init_worker_logging" with the set log_queue import re import logging.handlers import logging +from datetime import datetime import time from pathlib import Path from typing import MutableMapping, TextIO, TypedDict, Any, TYPE_CHECKING, cast @@ -23,6 +24,7 @@ class LogSettings(TypedDict): """log settings, for Log setup""" log_level_console: LoggingLevel log_level_file: LoggingLevel + per_run_log: bool console_enabled: bool console_color_output_enabled: bool add_start_info: bool @@ -392,6 +394,7 @@ class Log(LogParent): DEFAULT_LOG_SETTINGS: LogSettings = { "log_level_console": DEFAULT_LOG_LEVEL_CONSOLE, "log_level_file": DEFAULT_LOG_LEVEL_FILE, + "per_run_log": False, "console_enabled": True, "console_color_output_enabled": True, "add_start_info": True, @@ -440,7 +443,7 @@ class Log(LogParent): # in the file writer too, for the ones where color is set BEFORE the format # Any is logging.StreamHandler, logging.FileHandler and all logging.handlers.* self.handlers: dict[str, Any] = {} - self.add_handler('file_handler', self.__create_timed_rotating_file_handler( + self.add_handler('file_handler', self.__create_file_handler( 'file_handler', self.log_settings['log_level_file'], log_path) ) if self.log_settings['console_enabled']: @@ -492,6 +495,7 @@ class Log(LogParent): default_log_settings[__log_entry] = LoggingLevel.from_any(__log_level) # check bool for __log_entry in [ + "per_run_log", "console_enabled", "console_color_output_enabled", "add_start_info", @@ -566,24 +570,35 @@ class Log(LogParent): return console_handler # MARK: file handler - def __create_timed_rotating_file_handler( + def __create_file_handler( self, handler_name: str, log_level_file: LoggingLevel, log_path: Path, + # for TimedRotating, if per_run_log is off when: str = "D", interval: int = 1, backup_count: int = 0 - ) -> logging.handlers.TimedRotatingFileHandler: + ) -> logging.handlers.TimedRotatingFileHandler | logging.FileHandler: # file logger # when: S/M/H/D/W0-W6/midnight # interval: how many, 1D = every day # backup_count: how many old to keep, 0 = all if not self.validate_log_level(log_level_file): log_level_file = self.DEFAULT_LOG_LEVEL_FILE - file_handler = logging.handlers.TimedRotatingFileHandler( - filename=log_path, - encoding="utf-8", - when=when, - interval=interval, - backupCount=backup_count - ) + if self.log_settings['per_run_log']: + # log path, remove them stem (".log"), then add the datetime and add .log again + now = datetime.now() + # we add microseconds part to get milli seconds + new_stem=f"{log_path.stem}.{now.strftime('%Y-%m-%d_%H-%M-%S')}.{str(now.microsecond)[:3]}" + file_handler = logging.FileHandler( + filename=log_path.with_name(f"{new_stem}{log_path.suffix}"), + encoding="utf-8", + ) + else: + file_handler = logging.handlers.TimedRotatingFileHandler( + filename=log_path, + encoding="utf-8", + when=when, + interval=interval, + backupCount=backup_count + ) formatter_file_handler = logging.Formatter( ( # time stamp diff --git a/test-run/logging_handling/log.py b/test-run/logging_handling/log.py index e28a0de..dcd3cb5 100644 --- a/test-run/logging_handling/log.py +++ b/test-run/logging_handling/log.py @@ -24,6 +24,7 @@ def main(): # "log_level_console": None, "log_level_file": 'DEBUG', # "console_color_output_enabled": False, + "per_run_log": True } ) logn = Logger(log.get_logger_settings())