diff --git a/src/logging_conf.py b/src/logging_conf.py index 818e5003a916da19a787b4639a625c9b5b370a39..9672355bdc77c552e4904d192c537c785b127214 100644 --- a/src/logging_conf.py +++ b/src/logging_conf.py @@ -7,6 +7,45 @@ Description: this file contains a dictionary that allows to \ configure the logging module of python """ +from typing import Dict, Any +from pathlib import Path +import logging +import logging.config +import coloredlogs + + +class LoggingLevelError(Exception): + pass + + +def logging_def(output: Path, script: str, level: str = "INFO"): + """ + Define a logging at the current level of the script + + :param output: Folder where the result will be created + :param script: The name of the script + :param level: The log level + """ + possible_levels = ["INFO", "DEBUG", "ERROR", "WARNING", "CRITICAL"] + if level in possible_levels: + basename = str(Path(script).name).replace(".py", ".log") + LOGGING_CONFIG["handlers"]["file"]["filename"] = output / basename + LOGGING_CONFIG["loggers"][""]["level"] = level + logging.config.dictConfig(LOGGING_CONFIG) + elif level != "DISABLE": + raise LoggingLevelError(f"Logging level unknown : choose from " + f"{possible_levels} or DISABLE to disable the " + f"initialisation of logging in {__file__}") + + +coloredlogs.DEFAULT_LEVEL_STYLES = {'critical': {'color': 'red', 'bold': True}, + 'debug': {'color': 'cyan', 'italic': True, + 'bold': True}, + 'error': {'color': 'red', 'bold': True}, + 'info': {}, + 'warning': {'color': 'yellow', + 'bold': True}} + LOGGING_CONFIG = { 'version': 1, 'disable_existing_loggers': True, @@ -16,6 +55,7 @@ LOGGING_CONFIG = { 'levelname)s - %(message)s' }, "simple": { + '()': 'coloredlogs.ColoredFormatter', 'format': '%(message)s' } },