diff --git a/src/corelibs/logging_handling/log.py b/src/corelibs/logging_handling/log.py index c1fc0cf..f679fef 100644 --- a/src/corelibs/logging_handling/log.py +++ b/src/corelibs/logging_handling/log.py @@ -126,6 +126,7 @@ class Log: self.log_queue: 'Queue[str] | None' = None self.listener: logging.handlers.QueueListener | None = None + self.logger: logging.Logger | None = None # setup handlers # NOTE if console with color is set first, some of the color formatting is set @@ -219,6 +220,10 @@ class Log: """ if self.handlers.get(handler_name): return False + if self.listener is not None or self.logger is not None: + raise ValueError( + f"Cannot add handler {handler_name}: {handler.get_name()} because logger is already running" + ) # TODO: handler must be some handler type, how to check? self.handlers[handler_name] = handler return True @@ -360,6 +365,8 @@ class Log: *args (object): arguments for msg extra: Mapping[str, object] | None: extra arguments for the formatting if needed """ + if self.logger is None: + raise ValueError('Logger is not yet initialized') self.logger.log(LoggingLevel.EXCEPTION.value, msg, *args, exc_info=True, extra=extra) # MARK: break line @@ -370,6 +377,8 @@ class Log: Keyword Arguments: info {str} -- _description_ (default: {"BREAK"}) """ + if self.logger is None: + raise ValueError('Logger is not yet initialized') self.logger.info("[%s] %s>", info, self.SPACER_CHAR * self.SPACER_LENGTH) # MARK: queue handling @@ -432,13 +441,15 @@ class Log: self.handlers[handler_name].setLevel(log_level.name) return True except IndexError: - self.logger.error('Handler %s not found, cannot change log level', handler_name) + if self.logger: + self.logger.error('Handler %s not found, cannot change log level', handler_name) return False except AttributeError: - self.logger.error( - 'Cannot change to log level %s for handler %s, log level invalid', - LoggingLevel.name, handler_name - ) + if self.logger: + self.logger.error( + 'Cannot change to log level %s for handler %s, log level invalid', + LoggingLevel.name, handler_name + ) return False def get_log_level(self, handler_name: str) -> LoggingLevel: diff --git a/test-run/logging_handling/log.py b/test-run/logging_handling/log.py index 41f921c..7cb2102 100644 --- a/test-run/logging_handling/log.py +++ b/test-run/logging_handling/log.py @@ -23,6 +23,9 @@ def main(): # "console_color_output_enabled": False, } ) + if log.logger is None: + print("failed to start logger") + return log.logger.debug('[NORMAL] Debug test: %s', log.logger.name) log.logger.info('[NORMAL] Info test: %s', log.logger.name) diff --git a/test-run/logging_handling/log_pool.py b/test-run/logging_handling/log_pool.py index 963e71c..058bd61 100644 --- a/test-run/logging_handling/log_pool.py +++ b/test-run/logging_handling/log_pool.py @@ -10,6 +10,7 @@ import concurrent.futures import logging from pathlib import Path from corelibs.logging_handling.log import Log +from corelibs.logging_handling.logging_level_handling.logging_level import LoggingLevel def work_function(log_name: str, worker_id: int, data: list[int]) -> int: @@ -46,7 +47,10 @@ def main(): "log_queue": log_queue, } ) - log.logger.info('Pool Fork logging test') + if log.logger is None: + print("logger not yet started") + return + log.logger.debug('Pool Fork logging test') max_forks = 2 data_sets = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] with concurrent.futures.ProcessPoolExecutor( @@ -62,9 +66,23 @@ def main(): log.logger.info('Workders started') for future in concurrent.futures.as_completed(futures): - log.logger.info('Processing result: %s', future.result()) + log.logger.warning('Processing result: %s', future.result()) print(f"Processing result: {future.result()}") + log.set_log_level('stream_handler', LoggingLevel.ERROR) + log.logger.error('SECOND Start workers') + futures = [ + executor.submit(work_function, log.log_name, worker_id, data) + for worker_id, data in enumerate(data_sets, 1) + ] + log.logger.info('[INVISIBLE] Workders started') + log.logger.error('[VISIBLE] Second Workders started') + + for future in concurrent.futures.as_completed(futures): + log.logger.error('Processing result: %s', future.result()) + print(f"Processing result: {future.result()}") + + log.set_log_level('stream_handler', LoggingLevel.DEBUG) log.logger.info('[END] Queue logger test') log.stop_listener() diff --git a/test-run/logging_handling/log_queue.py b/test-run/logging_handling/log_queue.py index ab851a5..dfbe349 100644 --- a/test-run/logging_handling/log_queue.py +++ b/test-run/logging_handling/log_queue.py @@ -28,6 +28,9 @@ def main(): # "console_color_output_enabled": False, } ) + if log_q.logger is None: + print("failed to start logger") + return log_q.logger.debug('[QUEUE] Debug test: %s', log_q.logger.name) log_q.logger.info('[QUEUE] Info test: %s', log_q.logger.name)