secubox-deb/common/secubox_core/logger.py
CyberMind-FR bf1babb37c Initial commit: SecuBox-DEB migration from OpenWrt to Debian
Includes all package APIs with public dashboard endpoints:
- secubox-system: System Hub with /info, /resources, /security
- secubox-crowdsec: CrowdSec dashboard with /status, /hub, /metrics, actions
- secubox-wireguard: WireGuard VPN with /interfaces, /peers, start/stop
- secubox-netdata: Monitoring with /stats, /processes, /alerts

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-03-21 09:41:06 +01:00

56 lines
1.6 KiB
Python

"""
secubox_core.logger — Logging structuré JSON vers journald/stderr
=================================================================
Usage :
log = get_logger("crowdsec")
log.info("Ban appliqué: %s", ip)
log.warning("Échec connexion LAPI")
log.error("Timeout netifyd socket")
"""
from __future__ import annotations
import json
import logging
import os
import sys
import time
class _JsonFormatter(logging.Formatter):
"""Formate chaque log en JSON sur une ligne (parseable par journald)."""
def format(self, record: logging.LogRecord) -> str:
doc = {
"ts": int(time.time()),
"level": record.levelname,
"module": record.name,
"msg": record.getMessage(),
}
if record.exc_info:
doc["exc"] = self.formatException(record.exc_info)
return json.dumps(doc, ensure_ascii=False)
def get_logger(name: str, level: str | None = None) -> logging.Logger:
"""
Retourne un logger nommé `secubox.<name>`.
Level par défaut : WARNING en prod, DEBUG si SECUBOX_DEBUG=1.
"""
full_name = f"secubox.{name}"
log = logging.getLogger(full_name)
if log.handlers:
return log # déjà configuré
# Level
if level is None:
level = "DEBUG" if os.environ.get("SECUBOX_DEBUG") else "WARNING"
log.setLevel(getattr(logging, level.upper(), logging.WARNING))
# Handler vers stderr (capturé par journald via systemd)
h = logging.StreamHandler(sys.stderr)
h.setFormatter(_JsonFormatter())
log.addHandler(h)
log.propagate = False
return log