Add singleton logging configuration for the renamer application

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.
This commit is contained in:
sha
2026-01-05 14:54:03 +00:00
parent ad39632e91
commit 8031c97999
20 changed files with 350 additions and 109 deletions
+21 -2
View File
@@ -1,11 +1,12 @@
import json
import os
import threading
from pathlib import Path
from typing import Dict, Any
from typing import Dict, Any, Optional
class Settings:
"""Manages application settings stored in a JSON file."""
"""Manages application settings stored in a JSON file (Singleton)."""
DEFAULTS = {
"mode": "technical", # "technical" or "catalog"
@@ -17,12 +18,30 @@ class Settings:
"cache_ttl_posters": 2592000, # 30 days in seconds
}
_instance: Optional['Settings'] = None
_lock = threading.Lock()
def __new__(cls, config_dir: Path | None = None):
"""Create or return singleton instance."""
if cls._instance is None:
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialized = False
return cls._instance
def __init__(self, config_dir: Path | None = None):
"""Initialize settings (only once)."""
# Only initialize once
if self._initialized:
return
if config_dir is None:
config_dir = Path.home() / ".config" / "renamer"
self.config_dir = config_dir
self.config_file = self.config_dir / "config.json"
self._settings = self.DEFAULTS.copy()
self._initialized = True
self.load()
def load(self) -> None: