-->
Written by: Marlon Colca
Posted on 04 May 2025 - 11 days ago
python logging loguru
Local log files are great, but what happens when you scale?
In distributed systems, logs need to go to centralized services β like a log server, HTTP endpoint, or syslog daemon.
In this post, youβll learn how to stream Loguru logs across the network β no extra logging library needed.
Loguru lets you define any custom destination β called a sink.
That means you can send logs via:
import requests
from loguru import logger
def http_sink(message):
log = message.record
data = {
"time": log["time"].isoformat(),
"level": log["level"].name,
"message": log["message"],
"extra": log["extra"]
}
requests.post("https://your-log-endpoint.com/logs", json=data)
logger.remove()
logger.add(http_sink, level="INFO")
Now every log is sent as a JSON POST request.
import socket
from loguru import logger
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
target = ("your.syslog.server", 514)
def syslog_sink(message):
udp_socket.sendto(message.encode("utf-8"), target)
logger.remove()
logger.add(syslog_sink, level="INFO")
Send to any syslog-compatible daemon β even from Docker containers.
If youβre in an async app, wrap your sinks with enqueue=True:
logger.add(http_sink, enqueue=True)
Or use libraries like aiohttp for full async I/O if needed.
Network sinks can fail. Always add retry logic or use a queue-based solution like:
Loguruβs custom sinks make it easy to forward logs to any target β no need for extra dependencies or complex setups.
π Loguru + Sentry β Powerful Error Monitoring in Python
In this post, you'll learn how to connect Loguru with Sentry for full visibility over your app's issues.