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

Commit cc2063c

Browse files
committed
Add example.py for usage example
1 parent c65102c commit cc2063c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Log Handler to ship logs to Kafka Producer. Compatible with Django.
1818

1919

2020
#### Standalone Python
21+
- Refer `example.py`
2122
```
2223
class Main:
2324

python_logging_kafka/example.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import logging
2+
from python_logging_kafka import KafkaHandler
3+
4+
5+
class Main:
6+
7+
def __init__(self):
8+
logging.basicConfig(
9+
format='%(asctime)s %(levelname)s %(message)s',
10+
level=logging.INFO,
11+
datefmt='%m/%d/%Y %I:%M:%S %p'
12+
)
13+
self.logger = logging.getLogger('pylog')
14+
15+
ch = logging.StreamHandler()
16+
ch.setLevel(logging.INFO)
17+
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18+
ch.setFormatter(formatter)
19+
self.logger.addHandler(ch)
20+
21+
f1 = logging.FileHandler("pyblog.log")
22+
self.logger.addHandler(f1)
23+
24+
kh = KafkaHandler('localhost:9092', "pyblog")
25+
kh.setLevel(logging.INFO)
26+
self.logger.addHandler(kh)
27+
28+
def run(self):
29+
while True:
30+
log = input("> ")
31+
self.logger.info(log)
32+
33+
34+
if __name__ == '__main__':
35+
main = Main()
36+
main.run()

python_logging_kafka/pyblog.log

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
test

0 commit comments

Comments
 (0)