Skip to content

Kafka

KafkaHandler publishes log entries to a Kafka topic using confluent-kafka.

Installation

This integration requires the confluent-kafka package.

bash
uv add logly[kafka]
bash
pip install "logly[kafka]"
bash
uv add confluent-kafka
bash
pip install confluent-kafka

Missing Dependency

If confluent_kafka is not installed, you'll see:

ModuleNotFoundError: No module named 'confluent_kafka'

Usage

python
from logly import logger
from logly.integrations.kafka import KafkaHandler

handler = KafkaHandler(
    "localhost:9092",
    topic="app-logs",
    client_id="logly-producer",
)
logger.add(handler, level="WARNING")

Constructor Args

ArgumentTypeDefaultDescription
bootstrap_serversstrlocalhost:9092Comma-separated list of broker addresses
topicstrlogly-logsKafka topic to publish to
client_idstrlogly-producerClient identifier for the producer
acksstr"1"Acknowledgment mode: "all", "1", or "0"
timeoutfloat5.0Delivery timeout in seconds

Tips

  • Use acks="all" for maximum durability at the cost of latency.
  • Set a meaningful client_id for broker-side logging and debugging.

Full Example

python
from logly import logger
from logly.integrations.kafka import KafkaHandler

handler = KafkaHandler(
    bootstrap_servers="broker1:9092,broker2:9092",
    topic="myapp-logs",
    client_id="myapp-service",
    acks="all",
    timeout=10.0,
)
logger.add(handler, level="INFO")

logger.info("Processing request", request_id="req-123")
logger.error("Handler failed", error="timeout")

Released under the MIT License.