Add RFC 5424 syslog logging to all service templates
- decnet/logging/syslog_formatter.py: RFC 5424 formatter (local0 facility, decnet@55555 SD element ID, full escaping per §6.3.3) - decnet/logging/file_handler.py: rotating file handler (10 MB / 5 backups), path configurable via DECNET_LOG_FILE env var - templates/decnet_logging.py: combined syslog_line / write_syslog_file / forward_syslog helper distributed to all 22 service template dirs - All templates/*/server.py: replaced ad-hoc JSON _forward/_log with RFC 5424 syslog_line + write_syslog_file + forward_syslog - All templates/*/Dockerfile: COPY decnet_logging.py /opt/ - DecnetConfig: added log_file field; CLI: --log-file flag; composer injects DECNET_LOG_FILE env var into service containers - tests/test_syslog_formatter.py + tests/test_file_handler.py: 25 new tests Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -185,6 +185,7 @@ def deploy(
|
||||
distro: Optional[str] = typer.Option(None, "--distro", help="Comma-separated distro slugs, e.g. debian,ubuntu22,rocky9"),
|
||||
randomize_distros: bool = typer.Option(False, "--randomize-distros", help="Assign a random distro to each decky"),
|
||||
log_target: Optional[str] = typer.Option(None, "--log-target", help="Forward logs to ip:port (e.g. 192.168.1.5:5140)"),
|
||||
log_file: Optional[str] = typer.Option(None, "--log-file", help="Write RFC 5424 syslog to this path inside containers (e.g. /var/log/decnet/decnet.log)"),
|
||||
dry_run: bool = typer.Option(False, "--dry-run", help="Generate compose file without starting containers"),
|
||||
no_cache: bool = typer.Option(False, "--no-cache", help="Force rebuild all images, ignoring Docker layer cache"),
|
||||
config_file: Optional[str] = typer.Option(None, "--config", "-c", help="Path to INI config file"),
|
||||
@@ -233,6 +234,7 @@ def deploy(
|
||||
)
|
||||
|
||||
effective_log_target = log_target or ini.log_target
|
||||
effective_log_file = log_file
|
||||
decky_configs = _build_deckies_from_ini(
|
||||
ini, subnet_cidr, effective_gateway, host_ip, randomize_services
|
||||
)
|
||||
@@ -282,6 +284,7 @@ def deploy(
|
||||
distros_explicit=distros_list, randomize_distros=randomize_distros,
|
||||
)
|
||||
effective_log_target = log_target
|
||||
effective_log_file = log_file
|
||||
|
||||
config = DecnetConfig(
|
||||
mode=mode,
|
||||
@@ -290,6 +293,7 @@ def deploy(
|
||||
gateway=effective_gateway,
|
||||
deckies=decky_configs,
|
||||
log_target=effective_log_target,
|
||||
log_file=effective_log_file,
|
||||
)
|
||||
|
||||
if effective_log_target and not dry_run:
|
||||
|
||||
@@ -58,6 +58,8 @@ def generate_compose(config: DecnetConfig) -> dict:
|
||||
|
||||
fragment.setdefault("environment", {})
|
||||
fragment["environment"]["HOSTNAME"] = decky.hostname
|
||||
if config.log_file:
|
||||
fragment["environment"]["DECNET_LOG_FILE"] = config.log_file
|
||||
|
||||
# Share the base container's network — no own IP needed
|
||||
fragment["network_mode"] = f"service:{base_key}"
|
||||
|
||||
@@ -43,6 +43,7 @@ class DecnetConfig(BaseModel):
|
||||
gateway: str
|
||||
deckies: list[DeckyConfig]
|
||||
log_target: str | None = None # "ip:port" or None
|
||||
log_file: str | None = None # path for RFC 5424 syslog file output
|
||||
|
||||
@field_validator("log_target")
|
||||
@classmethod
|
||||
|
||||
57
decnet/logging/file_handler.py
Normal file
57
decnet/logging/file_handler.py
Normal file
@@ -0,0 +1,57 @@
|
||||
"""
|
||||
Rotating file handler for DECNET syslog output.
|
||||
|
||||
Writes RFC 5424 syslog lines to a local file.
|
||||
Path is controlled by the DECNET_LOG_FILE environment variable
|
||||
(default: /var/log/decnet/decnet.log).
|
||||
"""
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
_LOG_FILE_ENV = "DECNET_LOG_FILE"
|
||||
_DEFAULT_LOG_FILE = "/var/log/decnet/decnet.log"
|
||||
_MAX_BYTES = 10 * 1024 * 1024 # 10 MB
|
||||
_BACKUP_COUNT = 5
|
||||
|
||||
_handler: logging.handlers.RotatingFileHandler | None = None
|
||||
_logger: logging.Logger | None = None
|
||||
|
||||
|
||||
def _get_logger() -> logging.Logger:
|
||||
global _handler, _logger
|
||||
if _logger is not None:
|
||||
return _logger
|
||||
|
||||
log_path = Path(os.environ.get(_LOG_FILE_ENV, _DEFAULT_LOG_FILE))
|
||||
log_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
_handler = logging.handlers.RotatingFileHandler(
|
||||
log_path,
|
||||
maxBytes=_MAX_BYTES,
|
||||
backupCount=_BACKUP_COUNT,
|
||||
encoding="utf-8",
|
||||
)
|
||||
_handler.setFormatter(logging.Formatter("%(message)s"))
|
||||
|
||||
_logger = logging.getLogger("decnet.syslog")
|
||||
_logger.setLevel(logging.DEBUG)
|
||||
_logger.propagate = False
|
||||
_logger.addHandler(_handler)
|
||||
|
||||
return _logger
|
||||
|
||||
|
||||
def write_syslog(line: str) -> None:
|
||||
"""Write a single RFC 5424 syslog line to the rotating log file."""
|
||||
try:
|
||||
_get_logger().info(line)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def get_log_path() -> Path:
|
||||
"""Return the configured log file path (for tests/inspection)."""
|
||||
return Path(os.environ.get(_LOG_FILE_ENV, _DEFAULT_LOG_FILE))
|
||||
84
decnet/logging/syslog_formatter.py
Normal file
84
decnet/logging/syslog_formatter.py
Normal file
@@ -0,0 +1,84 @@
|
||||
"""
|
||||
RFC 5424 syslog formatter for DECNET.
|
||||
|
||||
Produces fully-compliant syslog messages:
|
||||
<PRI>1 TIMESTAMP HOSTNAME APP-NAME PROCID MSGID [SD-ELEMENT] MSG
|
||||
|
||||
Facility: local0 (16)
|
||||
PEN for structured data: decnet@55555
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
|
||||
FACILITY_LOCAL0 = 16
|
||||
NILVALUE = "-"
|
||||
_SD_ID = "decnet@55555"
|
||||
|
||||
SEVERITY_INFO = 6
|
||||
SEVERITY_WARNING = 4
|
||||
SEVERITY_ERROR = 3
|
||||
|
||||
# RFC 5424 field length limits
|
||||
_MAX_HOSTNAME = 255
|
||||
_MAX_APPNAME = 48
|
||||
_MAX_MSGID = 32
|
||||
|
||||
|
||||
def _pri(severity: int) -> str:
|
||||
return f"<{FACILITY_LOCAL0 * 8 + severity}>"
|
||||
|
||||
|
||||
def _truncate(value: str, maxlen: int) -> str:
|
||||
return value[:maxlen] if len(value) > maxlen else value
|
||||
|
||||
|
||||
def _sd_escape(value: str) -> str:
|
||||
"""Escape SD-PARAM-VALUE per RFC 5424 §6.3.3."""
|
||||
return value.replace("\\", "\\\\").replace('"', '\\"').replace("]", "\\]")
|
||||
|
||||
|
||||
def _sd_element(fields: dict[str, Any]) -> str:
|
||||
if not fields:
|
||||
return NILVALUE
|
||||
params = " ".join(
|
||||
f'{k}="{_sd_escape(str(v))}"'
|
||||
for k, v in fields.items()
|
||||
)
|
||||
return f"[{_SD_ID} {params}]"
|
||||
|
||||
|
||||
def format_rfc5424(
|
||||
service: str,
|
||||
hostname: str,
|
||||
event_type: str,
|
||||
severity: int = SEVERITY_INFO,
|
||||
timestamp: datetime | None = None,
|
||||
msg: str | None = None,
|
||||
**fields: Any,
|
||||
) -> str:
|
||||
"""
|
||||
Return a single RFC 5424-compliant syslog line (no trailing newline).
|
||||
|
||||
Args:
|
||||
service: APP-NAME field (e.g. "http", "mysql")
|
||||
hostname: HOSTNAME field (the decky node name)
|
||||
event_type: MSGID field (e.g. "request", "login_attempt")
|
||||
severity: Syslog severity integer (default: INFO=6)
|
||||
timestamp: Datetime to use; defaults to utcnow
|
||||
msg: Optional free-text MSG suffix
|
||||
**fields: Arbitrary key=value pairs encoded in structured data
|
||||
"""
|
||||
ts = (timestamp or datetime.now(timezone.utc)).isoformat()
|
||||
|
||||
pri = _pri(severity)
|
||||
version = "1"
|
||||
host = _truncate(hostname or NILVALUE, _MAX_HOSTNAME)
|
||||
appname = _truncate(service or NILVALUE, _MAX_APPNAME)
|
||||
procid = NILVALUE
|
||||
msgid = _truncate(event_type or NILVALUE, _MAX_MSGID)
|
||||
sd = _sd_element(fields)
|
||||
message = f" {msg}" if msg else ""
|
||||
|
||||
return f"{pri}{version} {ts} {host} {appname} {procid} {msgid} {sd}{message}"
|
||||
Reference in New Issue
Block a user