diff --git a/tdp/cli/__main__.py b/tdp/cli/__main__.py index 3ee50e1c..98d66d11 100644 --- a/tdp/cli/__main__.py +++ b/tdp/cli/__main__.py @@ -46,14 +46,13 @@ def load_env(ctx: click.Context, param: click.Parameter, value: Path) -> Optiona help="Path to environment configuration file.", ) @click.option( - "--log-level", - default="INFO", - envvar="TDP_LOG_LEVEL", - type=click.Choice(["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]), - help="Set the level of log output.", + "-d", + "--debug", + envvar="TDP_LOG_CONF_FILE", + help="Set the config file for the logging.", ) -def cli(env: Path, log_level: str): - setup_logging(log_level) +def cli(env: Path, debug: str): + setup_logging(debug) logging.info("Logging is configured.") diff --git a/tdp/cli/logger.py b/tdp/cli/logger.py index 2299262e..030e6dfd 100644 --- a/tdp/cli/logger.py +++ b/tdp/cli/logger.py @@ -1,35 +1,35 @@ # Copyright 2022 TOSIT.IO # SPDX-License-Identifier: Apache-2.0 -import logging +import logging.config DEFAULT_LOG_LEVEL = logging.INFO -def setup_logging(log_level: str) -> None: +def setup_logging(log_conf_file: str) -> None: """ Configure the logging module. Parameters: - log_level: The desired logging level as a string (e.g., "info"). + log_conf_file: The conf file for the logging. """ - # Ensure the provided log level is valid, using the default if it is not. - numeric_level = getattr(logging, log_level.upper(), None) - if not isinstance(numeric_level, int): - print(f"Invalid log level: {log_level}. Using {DEFAULT_LOG_LEVEL} instead.") + if log_conf_file: + logging.config.fileConfig(log_conf_file) + else: + # Default behaviour : using the default log level. numeric_level = DEFAULT_LOG_LEVEL - # Create a console handler with the specified log level - console_handler = logging.StreamHandler() - console_handler.setLevel(numeric_level) - # Create a formatter and attach it to the handler - formatter = logging.Formatter( - "%(asctime)s - %(levelname)s - %(name)s - %(message)s" - ) - console_handler.setFormatter(formatter) + # Create a console handler with the specified log level + console_handler = logging.StreamHandler() + console_handler.setLevel(numeric_level) + # Create a formatter and attach it to the handler + formatter = logging.Formatter( + "%(asctime)s - %(levelname)s - %(name)s - %(message)s" + ) + console_handler.setFormatter(formatter) - # Get the root logger and set its level to the specified level - logger = logging.getLogger() - logger.setLevel(numeric_level) - # Add the console handler to the logger - logger.addHandler(console_handler) + # Get the root logger and set its level to the specified level + logger = logging.getLogger() + logger.setLevel(numeric_level) + # Add the console handler to the logger + logger.addHandler(console_handler)