Coverage for rta_to_lstchain_formator/utils/rta_logging.py: 69%
22 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-25 05:21 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-25 05:21 +0000
1import logging
2import sys
3from enum import StrEnum
4from pathlib import Path
6# Enum class of logging levels, usefull to constraint the variable type in configuration json schema.
7LOGGING_LEVELS = StrEnum("LOGGING_LEVELS", {v: v for v in logging.getLevelNamesMapping().keys()})
10def log_uncaught_exceptions():
11 """Makes all uncaught exception to be logged by the default logger.
13 Keyboard exceptions and children classes are not logged so one can kill the program with ctr+C.
14 """
16 def handle_exception(exc_type, exc_value, exc_traceback):
17 if not issubclass(exc_type, KeyboardInterrupt):
18 logging.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))
20 sys.__excepthook__(exc_type, exc_value, exc_traceback)
21 return
23 sys.excepthook = handle_exception
26def init_logging(log_header_str: str, log_filename: Path | None, log_level: LOGGING_LEVELS):
27 """(Re-)initialize all loggers"""
29 logging.captureWarnings(True) # log all warnings from the warnings module.
30 log_uncaught_exceptions() # log all uncaught exceptions as well
32 logging_format = "%(asctime)s %(levelname)s {} %(pathname)s:%(lineno)s:%(funcName)s %(message)s".format(
33 log_header_str
34 )
35 handlers = []
36 if log_filename is not None: # write log to file 36 ↛ 37line 36 didn't jump to line 37 because the condition on line 36 was never true
37 handlers.append(logging.FileHandler(log_filename))
38 else: # write log to standard output and error
39 # WARNING: this could cause freezing due to output pipes been full if this is called as a subprocess !
40 handlers.append(logging.StreamHandler())
41 logging.basicConfig(
42 level=log_level.name,
43 format=logging_format,
44 handlers=handlers,
45 force=True,
46 )
48 logging.info("Logging configured - start logging")