logstash-formatter
raw JSON → 0.5.17 verified Mon Apr 27 auth: no python maintenance
A JSON log formatter for logstash, typically used with Python's logging module. Current version 0.5.17. Infrequently updated; last release in 2019.
pip install logstash-formatter Common errors
error ModuleNotFoundError: No module named 'logstash_formatter' ↓
cause Package not installed or import path incorrect.
fix
Run 'pip install logstash-formatter' and use 'from logstash_formatter import LogstashFormatter'.
error TypeError: __init__() got an unexpected keyword argument 'version' ↓
cause Passing parameters that don't match the constructor signature (e.g., 'version' instead of 'fmt' or 'datefmt').
fix
Check the constructor: LogstashFormatter(fmt=None, datefmt=None, extra=None). Remove unsupported arguments.
error AttributeError: 'LogstashFormatter' object has no attribute '_style' ↓
cause Incompatibility with Python logging internals in newer Python versions (3.8+).
fix
Pin to logstash-formatter==0.5.17 and ensure Python >=3.6. If issues persist, consider switching to python-json-logger.
Warnings
gotcha Message string is automatically included; do not embed JSON in the message itself as it may interfere with parsing. ↓
fix Use the extra parameter to add structured data instead of formatting it into the message string.
gotcha Timestamp defaults to the current time in UTC in ISO8601 format; timezone awareness may cause issues if your logs are not in UTC. ↓
fix Set the 'format' parameter to 'UNIX' or custom format, or ensure your logger's timezone is consistent.
deprecated Python 2 support is effectively abandoned; the library may not install or function correctly on Python 2. ↓
fix Upgrade to Python 3. Use version 0.5.17 on Python 3.6+.
Imports
- LogstashFormatter wrong
from logstash_formatter.formatter import LogstashFormattercorrectfrom logstash_formatter import LogstashFormatter
Quickstart
import logging
from logstash_formatter import LogstashFormatter
handler = logging.StreamHandler()
formatter = LogstashFormatter()
handler.setFormatter(formatter)
logger = logging.getLogger('my_logger')
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.info('Hello, logstash!', extra={'custom_field': 'value'})