Skip to content

lstautorta.utils.logging

Functions:

Name Description
init_logging

(Re-)initialize all loggers (Stream and file logger)

log_uncaught_exceptions

Makes all uncaught exception to be logged by the default logger.

init_logging

init_logging(log_level='DEBUG', log_filename='', format_title='LST AUTO RTA')

(Re-)initialize all loggers (Stream and file logger)

Parameters:

Name Type Description Default
log_level (str, Optional)

Log level of python logging's module to apply to all loggers. Default: DEBUG

'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"

'LST AUTO RTA'
Source code in src/lstautorta/utils/logging.py
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 = f"%(asctime)s - %(levelname)s - {format_title} - %(filename)s:%(lineno)s - %(message)s"
    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")

log_uncaught_exceptions

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.

Source code in src/lstautorta/utils/logging.py
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)

    sys.excepthook = handle_exception