add syslog

This commit is contained in:
larssand
2026-06-18 21:45:41 +02:00
parent b306679c60
commit e45603eed7
3 changed files with 48 additions and 1 deletions

23
src/fgai/syslog_server.py Normal file
View File

@@ -0,0 +1,23 @@
from __future__ import annotations
import socket
from pathlib import Path
def listen_udp_syslog(host: str, port: int, output: str, *, max_bytes: int = 65535) -> None:
output_path = Path(output)
output_path.parent.mkdir(parents=True, exist_ok=True)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
print(f"Listening for UDP syslog on {host}:{port}")
print(f"Writing logs to {output_path}")
with output_path.open("a", encoding="utf-8", buffering=1) as handle:
while True:
data, address = sock.recvfrom(max_bytes)
message = data.decode("utf-8", errors="replace").strip()
if not message:
continue
handle.write(f"{message}\n")
print(f"{address[0]}:{address[1]} {message}")