Source code for lst_auto_rta.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: str = "DEBUG", log_filename: str = "", format_title: str = "LST AUTO RTA"): """(Re-)initialize all loggers (Stream and file logger) Parameters ---------- log_level : str, Optional Log level of python logging's module to apply to all loggers. Default: DEBUG log_filename : str, Optional Path to the log file for the file logger. Default "" format_title : str, Optional Log line format "title" field. Default "LST AUTO RTA" """ 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 - {} - %(filename)s:%(lineno)s - %(message)s".format(format_title) logging_level = LOGGING_LEVELS(log_level) handlers = [logging.StreamHandler()] # output to stderr 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")