Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

Kafka Python Log Handler

Handler for the standard logging module which puts logs through to Kafka. The current implementation is very basic to accommodate our needs, but additional functionality may be coming when the parent project grows.

Install

This was developed in Python 3.6.7, using kafka-python 1.4.3 and Kafka 2.12

Development

Tests will try to connect to a Kafka instance via the defaults. Optionally setting KAFKA_HOST and KAFKA_PORT to the appropriate location on your machine will change this.

Install the dev requirements

    $ pip install -r requirements-dev.txt

Set the pre-commit hook

    $ pre-commit install

How to use

The only necessary thing to create this handler is a topic to push the values to. Optionally, a key and/or partition may be provided. Any additional keyword-value configuration provided to the Handler will be used to initialize the kafka.KafkaProducer.

To add a handler to the python logger is very simple:

import logging


from kafka_handler import KafkaLogHandler

handler = KafkaLogHandler(topic="example_topic", key="example_key")  # Default parameters for Kafka connection are used

logger = logging.getLogger()  # No name gives you the root logger
logger.setLevel("WARNING")
logger.addHandler(example_handler)

logger.warning("This will push this message to the 'example_topic' in Kafka.")

Configuring Kafka Connection

By default each handler will create a kafka.KafkaProducer instance, passing on each argument from their __init__(**kwargs) to the KafkaProducer's instantiation. This means you can configure the connection as specific as you'd like, but every argument should be provided with its keyword; Handler(host="localhost") instead of Handler("localhost"). All available configuration options are available in the kafka-python documentation.

handler = KafkaLogHandler(topic="topic", bootstrap_servers="other_host:9092", retries=0)

Configure message logging

Every handler has the raw_logging option which can be provided optionally. Omitting it from the initialisation, will default it to False, meaning the message being logged will be purely what's sent. If you set it to True, first the content will be logged, then appended to the line number and finally the pathname.

raw_handler = KafkaLogHandler(topic="topic", raw_logging=True)
...
logging.info("Test message")

A pure handler would emit a message like so: Test message.,
the raw_handler however, will emit a message like so: Test message. - 2: /.../file.py.

Release files for kafka-python-log-handler 0.0.1.dev12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kafka-python-log-handler 0.0.1.dev12
File Size Uploaded
kafka-python-log-handler-0.0.1.dev12.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for kafka-python-log-handler 0.0.1.dev12
File Interpreter ABI Platform
kafka_python_log_handler-0.0.1.dev12-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 11.2 kB

Release files / kafka-python-log-handler-0.0.1.dev12.tar.gz

Download URL kafka-python-log-handler-0.0.1.dev12.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0b748dffcea2a4ce4d1aaba1dded602925ade0a7168d3a096e67ffee7a9589a0
BLAKE2b-256 checksum
How to use checksums
4c564c94f82255b834b2f4ee9eadd90c1c3d87f48c1505a11608849288c5699c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release files / kafka_python_log_handler-0.0.1.dev12-py2.py3-none-any.whl

Download URL kafka_python_log_handler-0.0.1.dev12-py2.py3-none-any.whl
Size 4.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b9379b7ae47ba72ac4f46ff6a5d14266a4a8fa5c0ff244223e479996948ed9aa
BLAKE2b-256 checksum
How to use checksums
a7ba96c58e14b9695e2e6e82ca926008085fb27a8dfc184267e855cb1c59453f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.0.1.dev12 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page