News
This commit is contained in:
@@ -33,4 +33,28 @@ server:
|
||||
# Metadata
|
||||
meta:
|
||||
version: "0.1.0"
|
||||
environment: "dev"
|
||||
environment: "dev"
|
||||
|
||||
# Erweiterungen: interne/externe Logging-Module (01_Modulerweiterungen)
|
||||
logging_internal:
|
||||
enabled: true
|
||||
db_path: "data/internal_logs.sqlite"
|
||||
clean_database: false
|
||||
retention_days: 30
|
||||
max_entries: 100000
|
||||
vacuum_on_start: true
|
||||
batch_write: 100
|
||||
|
||||
logging_external:
|
||||
enabled: false
|
||||
type: "postgresql" # mysql | postgresql
|
||||
host: "localhost"
|
||||
port: 5432
|
||||
user: "logger"
|
||||
password: null # Secrets per Env-Var/Keystore, siehe [Security.md](01_Modulerweiterungen/Planung/Security.md:1)
|
||||
database: "logs"
|
||||
sslmode: "prefer"
|
||||
pool_size: 5
|
||||
connect_timeout: 10
|
||||
write_buffer_size: 100
|
||||
fallback_to_internal_on_error: true
|
||||
@@ -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."""
|
||||
|
||||
@@ -95,7 +95,7 @@ def app_factory() -> FastAPI:
|
||||
cfg = load_config_from_env()
|
||||
# Laufzeit-Settings laden und Logging initialisieren (überschreibt ggf. YAML-Logging)
|
||||
settings = load_runtime_config(base_dir=BASE_DIR)
|
||||
init_logging(settings)
|
||||
init_logging(settings, cfg)
|
||||
app = create_app(cfg)
|
||||
return app
|
||||
|
||||
@@ -135,8 +135,8 @@ def main(argv: Optional[List[str]] = None) -> None:
|
||||
cfg = load_yaml(config_path)
|
||||
# Laufzeit-Settings laden und Logging initialisieren (überschreibt ggf. YAML-Logging)
|
||||
settings = load_runtime_config(base_dir=BASE_DIR)
|
||||
init_logging(settings)
|
||||
|
||||
init_logging(settings, cfg)
|
||||
|
||||
app_cfg = cfg.get("app", {}) or {}
|
||||
host = str(app_cfg.get("host", "0.0.0.0"))
|
||||
port = int(app_cfg.get("port", 8000))
|
||||
|
||||
Reference in New Issue
Block a user