Logstash Python Formatter
This library provides a Python logging formatter designed to output log data as JSON objects compatible with Logstash json filters. It enables standard Python logging to produce structured logs that can be easily ingested and parsed by Logstash. The current version is 0.1.2, released in 2018, indicating a low or inactive release cadence.
Common errors
-
ModuleNotFoundError: No module named 'logstash_formatter'
cause The package was installed using a hyphenated name (`logstash-python-formatter`) or the import statement used a hyphenated name, but the actual installed package name on PyPI uses underscores.fixEnsure you installed with `pip install logstash_formatter` and imported with `from logstash_formatter import ...`. -
TypeError: Object of type <SomeObject> is not JSON serializable
cause You are trying to log an object that the standard `json` module cannot convert into a JSON string, often when passed in `extra` or directly as `msg`.fixThe `LogstashFormatter` has a `json_default` parameter. By default, it coerces unknown types to strings. If you have custom objects, either ensure they are converted to strings/JSON-serializable types before logging, or provide a custom `json_default` callable to the `LogstashFormatterV1` constructor to handle their serialization. -
Logstash shows 'msg' field instead of '@message' or 'message'
cause You might be using `LogstashFormatter` instead of `LogstashFormatterV1`, or your Logstash configuration expects a different field name for the message.fixUse `LogstashFormatterV1` explicitly as it handles the `msg` to `message` conversion for Logstash 1.2.0 schema. Ensure your Logstash input configuration is set up to parse JSON correctly and expects the `message` field.
Warnings
- gotcha The PyPI package name is `logstash_formatter` (with an underscore) while the library is often referred to as `logstash-python-formatter` (with a hyphen). Ensure `pip install logstash_formatter` and `from logstash_formatter import ...` for correct usage.
- gotcha When using `LogstashFormatterV1`, the original `msg` field in a LogRecord is specially handled and updated to `message` in the output JSON. If `msg` is a dictionary, its contents are merged as top-level fields instead of being treated as a message string.
- gotcha The keys in the `extra` dictionary passed to logging calls should not clash with standard LogRecord attributes (e.g., `name`, `levelname`, `lineno`). Overlapping keys might lead to unexpected behavior or overwriting of standard fields.
- deprecated This library (version 0.1.2, last updated 2018) appears to be in maintenance mode or largely inactive. More actively developed alternatives like `python-logstash` or `python-logstash-async` might offer broader features, better performance (e.g., asynchronous handling), and more current Logstash schema support.
Install
-
pip install logstash_formatter
Imports
- LogstashFormatterV1
from logstash_python_formatter import LogstashFormatterV1
from logstash_formatter import LogstashFormatterV1
Quickstart
import logging
from logstash_formatter import LogstashFormatterV1
import sys
# Configure a logger
logger = logging.getLogger('my_app')
logger.setLevel(logging.INFO)
# Create a StreamHandler to output logs to console
handler = logging.StreamHandler(sys.stdout)
# Create an instance of LogstashFormatterV1
# You can pass 'fmt' as a JSON string to add default extra fields or override source_host
# For example: formatter = LogstashFormatterV1(fmt='{"extra": {"service": "my-service"}}')
formatter = LogstashFormatterV1()
# Set the formatter for the handler
handler.setFormatter(formatter)
# Add the handler to the logger
logger.addHandler(handler)
# Log messages
logger.info('This is a simple info message.')
logger.warning('This is a warning message with some context.', extra={'user_id': 123, 'transaction_id': 'abc-123'})
try:
1 / 0
except ZeroDivisionError:
logger.error('An error occurred!', exc_info=True, extra={'error_code': 500})
# Logging a dictionary directly as a message (fields are merged into the top level)
logger.info({'event': 'user_login', 'username': 'testuser'})