Source code for rta_reconstruction.utils.logging

import logging
import sys
from enum import Enum

# Mapping between logging levels exposed to users, and logging levels in software
# Users level should eventually follow ACADA AE Logging ICD https://redmine.cta-observatory.org/dmsf/files/14915/view
LOGGING_LEVELS_DICT = {
    "DEBUG": logging.DEBUG,
    "INFO": logging.INFO,
    "WARNING": logging.WARNING,
    "ERROR": logging.ERROR,
    "CRITICAL": logging.CRITICAL,
}

# Enum class of logging levels, to constraint the variable type in configuration json schema.
LOGGING_LEVELS = Enum("LOGGING_LEVELS", {v: v for v in LOGGING_LEVELS_DICT.keys()})


[docs] def log_uncaught_exceptions(): """Makes all uncaught exception to be logged by the default logger. Keyboard exceptions and children classes are not logged so one can kill the program with ctr+C. """ def handle_exception(exc_type, exc_value, exc_traceback): if not issubclass(exc_type, KeyboardInterrupt): logging.critical("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) sys.__excepthook__(exc_type, exc_value, exc_traceback) return sys.excepthook = handle_exception
[docs] def init_logging(log_level="DEBUG", log_filename="dl1_to_dl2.log"): """(Re-)initialize all loggers""" logging.captureWarnings(True) # log all warnings from the warnings module. log_uncaught_exceptions() # log all uncaught exceptions as well logging_format = "%(asctime)s - %(levelname)s - SAG DL1 to DL2 - %(filename)s:%(lineno)s - %(message)s" logging_level = LOGGING_LEVELS(log_level) # This is deactivated as it makes logging call randomly deadlock in jenkins CI machine # TODO: issue # output to stderr # handlers = [logging.StreamHandler()] # output to stderr handlers = [] if log_filename: # and also output to file if asked handlers.append(logging.FileHandler(log_filename)) logging.basicConfig( level=logging_level.name, format=logging_format, handlers=handlers, force=True, ) logging.info("Logging configured - start logging")