ECS Logging for Python

2.3.0 · active · verified Fri Apr 10

ecs-logging-python provides logging formatters for Python's standard `logging` module and `structlog`, enabling applications to produce logs compliant with the Elastic Common Schema (ECS). It helps in standardizing log formats for easier ingestion and analysis in Elastic Stack products like Elasticsearch and Kibana. The library is currently at version 2.3.0 and is actively maintained, with a focus on adding new features and supporting recent Python versions.

Warnings

Install

Imports

Quickstart

This example demonstrates how to configure the standard Python `logging` module to output ECS-compliant JSON logs to `stdout`. It includes an example of adding custom fields via the `extra` dictionary and logging an exception.

import logging
import ecs_logging
import sys

# Get the Logger
logger = logging.getLogger('app')
logger.setLevel(logging.DEBUG)

# Add an ECS formatter to the Handler
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(ecs_logging.StdlibFormatter())
logger.addHandler(handler)

# Emit a log!
logger.debug('Example message!', extra={'http.request.method': 'get', 'user.name': 'john.doe'})

try:
    1 / 0
except ZeroDivisionError:
    logger.error('An error occurred!', exc_info=True)

view raw JSON →