Source code for fabex.utilities.logging_utils

"""Fabex 'logging_utils.py' © 2025
"""

from datetime import datetime
import logging
from pathlib import Path

[docs] current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
[docs] log = logging.getLogger("fabex_logger")
log.setLevel(logging.DEBUG) # Create file handler for logging
[docs] log_path = Path(__file__).parent.parent / "logs" / f"{current_time}.log"
[docs] file_handler = logging.FileHandler(log_path)
file_handler.setLevel(logging.DEBUG) # Create another file handler for error logging
[docs] error_log_path = Path(__file__).parent.parent / "logs" / f"Error_{current_time}.log"
[docs] error_handler = logging.FileHandler(error_log_path)
error_handler.setLevel(logging.ERROR) # Create console handler to pass messages to console
[docs] console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) # Define custom format for the log messages
[docs] file_formatter = logging.Formatter( "%(asctime)s | %(levelname)8s: %(message)s", datefmt="%Y-%m-%d %H:%M:%S", )
[docs] error_formatter = logging.Formatter( "%(asctime)s | %(levelname)s: %(message)s | File '%(pathname)s', line %(lineno)d in %(funcName)s", datefmt="%Y-%m-%d %H:%M:%S", )
[docs] console_formatter = logging.Formatter( "%(message)s", )
file_handler.setFormatter(file_formatter) error_handler.setFormatter(error_formatter) console_handler.setFormatter(console_formatter) # Adding handlers to the logger log.addHandler(file_handler) log.addHandler(error_handler) log.addHandler(console_handler)