mirror of
https://github.com/shadoll/moma.git
synced 2026-08-28 03:27:34 +00:00
This commit introduces a new module `logging_config.py` that implements a singleton pattern for logging configuration. The logger is initialized only once and can be configured based on an environment variable to log to a file or to the console. This centralizes logging setup and ensures consistent logging behavior throughout the application.
47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
"""Singleton logging configuration for the renamer application.
|
|
|
|
This module provides centralized logging configuration that is initialized
|
|
once and used throughout the application.
|
|
"""
|
|
|
|
import logging
|
|
import os
|
|
import threading
|
|
|
|
|
|
class LoggerConfig:
|
|
"""Singleton logger configuration."""
|
|
|
|
_instance = None
|
|
_lock = threading.Lock()
|
|
_initialized = False
|
|
|
|
def __new__(cls):
|
|
"""Create or return singleton instance."""
|
|
if cls._instance is None:
|
|
with cls._lock:
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
return cls._instance
|
|
|
|
def __init__(self):
|
|
"""Initialize logging configuration (only once)."""
|
|
if LoggerConfig._initialized:
|
|
return
|
|
|
|
# Check environment variable for formatter logging
|
|
if os.getenv('FORMATTER_LOG', '0') == '1':
|
|
logging.basicConfig(
|
|
filename='formatter.log',
|
|
level=logging.DEBUG,
|
|
format='%(asctime)s - %(levelname)s - %(message)s'
|
|
)
|
|
else:
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
LoggerConfig._initialized = True
|
|
|
|
|
|
# Initialize logging on import
|
|
LoggerConfig()
|