Skip to content
This repository was archived by the owner on Jun 29, 2022. It is now read-only.

Commit e4dabad

Browse files
committed
Python-Kafka logging package
0 parents  commit e4dabad

File tree

10 files changed

+86
-0
lines changed

10 files changed

+86
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
__pycache__
3+
*.pyc

MANIFEST.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include *.txt
2+
include *.md
3+
include *.rst

README.md

Whitespace-only changes.

python_logging_kafka/__init__.py

Whitespace-only changes.

python_logging_kafka/consumer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from kafka import KafkaConsumer
2+
consumer = KafkaConsumer('pyblog')
3+
for message in consumer:
4+
print (message)

python_logging_kafka/handlers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from logging import StreamHandler
2+
from kafka import KafkaProducer
3+
4+
5+
class KafkaHandler(StreamHandler):
6+
7+
def __init__(self, broker, topic):
8+
StreamHandler.__init__(self)
9+
self.broker = broker
10+
self.topic = topic
11+
12+
self.kafka_broker = MyKafka(broker)
13+
14+
def emit(self, record):
15+
message = self.format(record)
16+
self.kafka_broker.send(message, self.topic)
17+
18+
19+
class MyKafka(object):
20+
21+
def __init__(self, kafka_brokers, json=False):
22+
self.json = json
23+
if not json:
24+
self.producer = KafkaProducer(bootstrap_servers=kafka_brokers)
25+
else:
26+
self.producer = KafkaProducer(value_serializer=lambda v: json.dumps(v).encode('utf-8'),
27+
bootstrap_servers=kafka_brokers)
28+
29+
def send(self, data, topic):
30+
if self.json:
31+
result = self.producer.send(topic, key=b'log', value=data)
32+
else:
33+
result = self.producer.send(topic, bytes(data, 'utf-8'))
34+
print("kafka send result: {}".format(result.get()))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
kafka-python==2.0.1

setup.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import pathlib
2+
from setuptools import setup
3+
4+
HERE = pathlib.Path(__file__).parent
5+
README = (HERE / "README.md").read_text()
6+
7+
CLASSIFIERS = [
8+
'Intended Audience :: Developers',
9+
'License :: OSI Approved :: MIT License',
10+
'Operating System :: OS Independent',
11+
'Programming Language :: Python',
12+
'Programming Language :: Python :: 3.6',
13+
'Programming Language :: Python :: 3.7',
14+
]
15+
16+
INSTALL_REQUIRES = [
17+
'kafka-python>=2.0.1'
18+
]
19+
20+
TEST_REQUIRES = [
21+
"pytest>=4.3.0",
22+
]
23+
24+
setup(name='python-logging-kafka',
25+
version='1.0.0',
26+
url='https://github.com/albertomr86/python-logging-rabbitmq',
27+
description='Kafka log producer from Python/Django',
28+
long_description=README,
29+
keywords='logging kafka logs',
30+
license='MIT',
31+
author='Adesh Nalpet',
32+
author_email="390.adesh@gmail.com",
33+
classifiers=CLASSIFIERS,
34+
python_requires="!=3.5.*, !=3.6.*, !=3.7.*",
35+
install_requires=INSTALL_REQUIRES,
36+
include_package_data=True,
37+
packages=['python_logging_kafka'],
38+
test_suite='tests',
39+
zip_safe=True)

tests/__init__.py

Whitespace-only changes.

tests/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def mock_test():
2+
assert True == True

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy