add syslog
This commit is contained in:
23
src/fgai/syslog_server.py
Normal file
23
src/fgai/syslog_server.py
Normal 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}")
|
||||
Reference in New Issue
Block a user