Registry / observability / ecs-logging

ecs-logging

JSON →
library2.3.0pypypi✓ verified 26d ago

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.

pip install ecs-logging
INSTALL
IMPORT
SIG · ECS-LOGGING
E
ecs-logging
observabilitypythonv2.3.0
Install
1.6s avg
Import
38ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.040s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.036s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

StdlibFormatter
from ecs_logging import StdlibFormatter
StructlogFormatter
from ecs_logging import StructlogFormatter
from ecs_logging.structlog import StructlogFormatter
The `StructlogFormatter` is directly available under the `ecs_logging` top-level package since version 1.0.0.

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)
Debug
Known issues
breakingPython 2 support was removed in version 2.0.0. Ensure your projects are running on Python 3.
fix
Upgrade your Python environment to Python 3.6+ (v2.0.0 required >=3.6) or specifically >=3.8 for current versions.
affects: >=2.0.0
breakingPython 3.5 support was removed in version 1.1.0.
fix
Upgrade your Python environment to Python 3.6+.
affects: >=1.1.0
gotchaWhen using `StdlibFormatter`, additional ECS fields must be passed via the `extra` dictionary argument to logging calls. Keys with dot notation (e.g., `user.name`) will automatically be de-dotted and nested in the output JSON.
fix
Always use `logger.info('message', extra={'field.subfield': 'value'})` for custom ECS fields.
affects: All versions
gotchaIf using `structlog`, the `ecs_logging.StructlogFormatter` must be the *last* processor in your `structlog.configure` processor list, as it handles the final JSON conversion and ECS field enrichment.
fix
Ensure `ecs_logging.StructlogFormatter()` is the final item in the `processors` list when configuring `structlog`.
affects: All versions
gotchaThe `ensure_ascii` parameter for `StdlibFormatter` and `StructlogFormatter` (introduced in v2.3.0) controls whether non-ASCII characters in log messages are escaped to `\uXXXX` sequences. It defaults to `True`.
fix
If you need unescaped non-ASCII characters (e.g., for direct display in certain terminals or systems), initialize the formatter with `ensure_ascii=False`: `ecs_logging.StdlibFormatter(ensure_ascii=False)`.
affects: >=2.3.0
gotchaPrior to v1.0.2, `StdlibFormatter` had a signature mismatch with `logging.Formatter` that could cause issues in frameworks like Django and Gunicorn.
fix
Upgrade to version 1.0.2 or higher.
affects: <1.0.2
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'ecs_logging'
The `ecs-logging` Python package is not installed in the active Python environment or is not accessible in the current project's `PYTHONPATH`.
fix
Ensure the library is installed using pip: `python -m pip install ecs-logging`
TypeError: 'exclude_fields' must be a sequence of strings
The `exclude_fields` parameter of `ecs_logging.StdlibFormatter` or `ecs_logging.StructlogFormatter` was not provided as a sequence (e.g., a list or tuple) of strings, or an individual string was passed instead of a sequence containing a string.
fix
Pass `exclude_fields` as a list of strings, for example: `formatter = ecs_logging.StdlibFormatter(exclude_fields=['log.original', 'process'])`
mapper_parsing_exception: failed to parse field [client.ip] of type [ip] in document with id '...'. Preview of field's value: 'unavailable'
Logs formatted by `ecs-logging-python` contain values for ECS fields that do not match the expected data type in Elasticsearch's mapping. For instance, a non-IP string ('unavailable') was provided for a field mapped as an 'ip' type.
fix
Ensure that the data provided to your logger (especially in the `extra` dictionary) for specific ECS fields adheres to the expected data types defined by the Elastic Common Schema and your Elasticsearch index mappings. Validate input data before logging it to prevent type mismatches (e.g., ensure `client.ip` is a valid IP address or null/absent if unknown).
Upgrade
Version history
2.3.0latest on PyPI · released Jan 19, 2026
Audit
Dependencies
elastic-apmoptionalOptional dependency for automatic collection of ECS tracing fields to correlate logs with spans, transactions, and traces in Elastic APM.
structlogoptionalRequired if using the `ecs_logging.StructlogFormatter` for structured logging with `structlog`.
Agent activity
22 hits · last 30 days
node
18
OpenAI (training)
2
Amazon
1
Resources
ecs-logging — pip install ecs-logging · libregistry