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