Logging integrationsedit

The Elastic APM agent provides integrations with both the default Python logging library, as well as structlog.


For Python 3.2+, we use logging.setLogRecordFactory() to decorate the default LogRecordFactory to automatically add new attributes to each LogRecord object:

  • elasticapm_transaction_id
  • elasticapm_trace_id
  • elasticapm_span_id

This factory also adds these fields to a dictionary attribute, elasticapm_labels, using the official ECS tracing fields.

You can disable this automatic behavior by using the disable_log_record_factory setting in your configuration

For Python versions <3.2, we also provide a filter which will add the same new attributes to any filtered LogRecord:

Note: Because filters are not propagated to descendent loggers, you should add the filter to each of your log handlers, as handlers are propagated, along with their attached filters.

import logging
from elasticapm.handlers.logging import LoggingFilter

console = logging.StreamHandler()
# add the handler to the root logger


We provide a processor for structlog which will add three new keys to the event_dict of any processed event:

  • transaction.id
  • trace.id
  • span.id
from structlog import PrintLogger, wrap_logger
from elasticapm.handlers.structlog import structlog_processor

wrapped_logger = PrintLogger()
logger = wrap_logger(wrapped_logger, processors=[structlog_processor])
log = logger.new()

Use structlog for agent-internal loggingedit

The Elastic APM Python agent uses logging to log internal events and issues. By default, it will use a logging logger. If your project uses structlog, you can tell the agent to use a structlog logger by setting the environment variable ELASTIC_APM_USE_STRUCTLOG to true.