The Astro logo on a dark background with rainbow rays.

Sending Logs Over the Network with Loguru

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.


πŸ“€ Use a Custom Sink

Loguru lets you define any custom destination β€” called a sink.

That means you can send logs via:

  • HTTP (to services like Logtail, Datadog, Logstash)
  • TCP/UDP (to a log collector)
  • WebSocket
  • External tools or APIs

πŸš€ Example 1: Send Logs via HTTP POST

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.

πŸ§ͺ Example 2: Send Logs via UDP to Syslog

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.

πŸ”„ Async-Friendly Sinks

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.

🧠 Tip: Use Retry + Backoff

Network sinks can fail. Always add retry logic or use a queue-based solution like:

  • Celery workers
  • Redis pub/sub
  • Logstash agents with buffering

βœ… Final Thoughts

Loguru’s custom sinks make it easy to forward logs to any target β€” no need for extra dependencies or complex setups.

πŸ”œ Coming up next

πŸ‘‰ Loguru + Sentry β€” Powerful Error Monitoring in Python


πŸ”œ Coming up next


The Astro logo on a dark background with rainbow rays.

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.