This commit is contained in:
2025-11-13 11:11:21 +01:00
parent 4830cafd5a
commit e02c3b79cc
7 changed files with 1129 additions and 26 deletions

View File

@@ -5,7 +5,7 @@ import logging.handlers
import os
from datetime import datetime, timedelta
from pathlib import Path
from typing import Iterable, List, Tuple
from typing import Iterable, List, Tuple, Dict, Optional, Any
from .config_loader import Settings
@@ -14,7 +14,7 @@ DEFAULT_FORMAT = "%(asctime)s | %(levelname)s | %(name)s | %(filename)s:%(lineno
DEFAULT_DATEFMT = "%Y-%m-%d %H:%M:%S"
def init_logging(settings: Settings) -> None:
def init_logging(settings: Settings, app_config: Optional[Dict[str, Any]] = None) -> None:
"""
Initialisiert das Logging gemäß den Laufzeit-Settings.
@@ -78,6 +78,45 @@ def init_logging(settings: Settings) -> None:
except Exception: # Schutz vor Start-Abbruch durch Aufräumfehler
logging.getLogger(__name__).exception("Fehler beim Log-Cleanup ignoriert.")
# Optionale Integration: internes SQLite-Logging (01_Modulerweiterungen)
try:
internal_cfg = ((app_config or {}).get("logging_internal") or {})
except Exception:
internal_cfg = {}
if isinstance(internal_cfg, dict) and internal_cfg.get("enabled"):
try:
import importlib
mod = importlib.import_module("logging_internal")
except Exception:
logging.getLogger(__name__).warning(
"logging_internal Modul nicht importierbar; interner DB-Handler wird nicht aktiviert."
)
else:
try:
db_path = internal_cfg.get("db_path", "data/internal_logs.sqlite")
clean_db = bool(internal_cfg.get("clean_database", False))
retention_days_cfg = int(internal_cfg.get("retention_days", 30))
max_entries_cfg = int(internal_cfg.get("max_entries", 100000))
vacuum_on_start = bool(internal_cfg.get("vacuum_on_start", True))
resolved_db_path = settings.resolve_path(str(db_path))
mod.init(
db_path=str(resolved_db_path),
vacuum_on_start=vacuum_on_start,
clean_database=clean_db,
retention_days=retention_days_cfg,
max_entries=max_entries_cfg,
)
handler = mod.get_engineered_handler(level)
root.addHandler(handler)
logging.getLogger(__name__).info("Interner SQLite-Log-Handler aktiv: %s", str(resolved_db_path))
except Exception:
logging.getLogger(__name__).exception(
"Fehler bei Initialisierung des internen SQLite-Loggings; Handler wird nicht aktiviert."
)
def _reset_logger_handlers(logger: logging.Logger) -> None:
"""Entfernt alle existierenden Handler vom Logger."""