blob: a52a12055b9ca7d3e8eff38b98aed86d2e6ad7f6 [file] [log] [blame]
Zack Williams9e8efd32018-10-17 15:01:13 -07001KafkaLogHandler
2===============
3
4Provides a python ``logging`` compatible handler for producing messages to a
5Kafka message bus.
6
7Depends on the confluent_kafka module to connect to Kafka
8
9Designed to support structured logging, and serializes log data as JSON when
10published as a Kafka message.
11
12Usage
13=====
14
15**Example:**
16
17::
18
19 import logger
20
21 from kafkaloghandler import KafkaLogHandler
22
23 log = logging.getLogger()
24
25 klh = KafkaLogHandler(bootstrap_servers=["test-kafka:9092"], topic="testtopic")
26
27 log.addHandler(klh)
28
29 data={'example':'structured data'}
30
31 log.info('message to send to kafka', data=data)
32
33
34**Parameters that can be provided to KafkaLogHandler:**
35
36*bootstrap_servers*
37 List of Kafka bootstrap servers to connect to. See confluent_kafka docs.
38
39 **default:** ``["localhost:9092"]``
40
41*timeout*
42 Timeout in seconds for flushing producer queue. See confluent_kafka docs.
43
44 **default:** ``10.0``
45
46*topic*
47 String that sets the topic in Kafka.
48
49 **default:** ``"kafkaloghandler"``
50
51*key*
52 String that sets the default key in Kafka, can be used for summarization within Kafka.
53
54 NOTE: This default key can be overridden on a per-message basis by passing a
55 dict to the logger with ``{"key": "new_key_for_this_message"}`` in it.
56
57 **default:** ``"klh"``
58
59*flatten*
60 Flattens nested dictionary keys passed as structured logging into the parent
61 dictionary layer, up to a certain depth.
62
63 This is useful when logging to external systems that don't have good support
64 for hierarchical data.
65
66 Example: ``{'a': {'b': 'c'}}`` would be flattened to ``{'a.b': 'c'}``
67
68 If the depth is exceeded, any remaining deeper dict will be added to the
69 output under the flattened key.
70
71 Set to ``0`` to turn off flattening.
72
73 **default:** ``5``
74
75*separator*
76 Separator used between keys when flattening.
77
78 **default:** ``.``
79
80*blacklist*
81 List of top-level keys to discard from structured logs when outputting JSON.
82
83 **default:** ``['_logger']``
84
85
86Tests
87=====
88
89Unit tests can be run with:
90
91 nose2 --verbose --coverage-report term