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:
2026-04-04 04:31:00 -03:00
parent cf1e00af28
commit 55896b0caa
76 changed files with 3800 additions and 422 deletions

View 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}"