eht16 / python-logstash-async

Python logging handler for sending log events asynchronously to Logstash.
MIT License
186 stars 51 forks source link

Add support for adding callables as extra fields #95

Open piotrmaslanka opened 7 months ago

piotrmaslanka commented 7 months ago

Support was added for specifying callables as extra fields. This is super useful if we're trying to attach span_id and trace_id to a log entry in order to cross-match it with a tracing system, such as Jaeger, and we need that data dynamically rather than statically at startup.

Example:

get_context = lambda: tracer.active_span.context
add_trace_id = lambda: hex(get_context().trace_id)[2:] if tracer is not None and tracer.active_span is not None else None
add_span_id = lambda: hex(get_context().span_id)[2:] if tracer is not None and tracer.active_span else None

formatter = LogstashFormatter(extra={'span_id': add_span_id, 'trace_id': add_trace_id})
logging.getLogger().setFormatter(formatter)