Skip to content

feat: Define log level based on a config file #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions tdp/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")


Expand Down
40 changes: 20 additions & 20 deletions tdp/cli/logger.py
Original file line number Diff line number Diff line change
@@ -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)