Source code for do_dpc.utils.logging_config
"""
Configures application-wide logging with colored console output and file logging.
"""
import logging
from do_dpc.utils.path_manager import get_path_manager
# Get a singleton instance of PathManager
path_manager = get_path_manager()
[docs]
def setup_logging():
"""
Configures logging using the log file path from PathManager.
Uses colored logs for console output and plain text for file logs.
"""
log_file = path_manager.get_log_file(with_date=True)
log_format = "%(asctime)s - %(levelname)s - %(name)s - %(message)s"
# Create formatter for file logging (plain text)
file_formatter = logging.Formatter(log_format)
# Create formatter for console logging (with colors)
colored_formatter = SimpleColoredFormatter(log_format)
file_handler = logging.FileHandler(log_file, mode="a")
file_handler.setFormatter(file_formatter)
console_handler = logging.StreamHandler()
console_handler.setFormatter(colored_formatter)
logging.basicConfig(
level=logging.INFO,
handlers=[file_handler, console_handler],
)
[docs]
def get_logger(name: str) -> logging.Logger:
"""
Returns a logger for a specific module.
Args:
name (str): The name of the logger (typically `__name__`).
Returns:
logging.Logger: Configured logger instance.
"""
return logging.getLogger(name)
# Automatically set up logging when this module is imported
setup_logging()