eptm_dashboard/src/logger.py
2026-05-09 23:27:46 +02:00

27 lines
896 B
Python

import os
from datetime import datetime
from pathlib import Path
from zoneinfo import ZoneInfo
_TZ = ZoneInfo("Europe/Zurich")
_ROOT = Path(__file__).resolve().parent.parent
DATA_DIR = Path(os.getenv("DATA_DIR", str(_ROOT / "data")))
_LOG_FILE = DATA_DIR / "logs" / "operations.log"
def app_log(msg: str, debug: bool = False) -> None:
"""Écrit une entrée dans operations.log.
PROD (debug=False) : [HH:MM:SS] msg — ligne non indentée, visible en mode PROD
DEBUG (debug=True) : [HH:MM:SS] msg — ligne indentée, visible seulement en mode DEBUG
"""
ts = datetime.now(tz=_TZ).strftime("%H:%M:%S")
indent = " " if debug else ""
line = f"[{ts}] {indent}{msg}"
try:
_LOG_FILE.parent.mkdir(parents=True, exist_ok=True)
with _LOG_FILE.open("a", encoding="utf-8") as f:
f.write(line + "\n")
except Exception:
pass