logfmter is a Python package that enables logfmt-formatted logging using the standard `logging` module without requiring changes to existing log calls. It aims to provide human and machine-readable logs, adhering to best practices like those recommended by Splunk. The current version is 0.0.12, and new releases are typically made for bug fixes, Python version support, and minor feature enhancements.
pip install logfmterVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to integrate `logfmter` with the standard Python `logging` module using `basicConfig`. It sets up a `StreamHandler` to output logfmt-formatted messages to the console, including `extra` dictionary parameters which `logfmter` automatically flattens into key-value pairs. It also shows logging a dictionary as the main message.
Upgrade Python to 3.10 or newer, or pin `logfmter` dependency to `<0.0.12`.
Adjust logging messages to conform to the single-line logfmt standard. Avoid embedding newline characters or adapt your log parsing to handle escaped newlines.
Review log parsing tools to ensure they correctly interpret properly escaped backslashes. No code change is typically needed unless relying on the prior incorrect escaping behavior.
If you need existing loggers (e.g., from third-party libraries) to continue logging, explicitly set `"disable_existing_loggers": false` within your `dictConfig` dictionary.
Use `logging.basicConfig` for simple setups or `logging.config.dictConfig` for more complex logging configurations.
Ensure a `Logfmter` instance is set as the formatter for at least one `StreamHandler` or `FileHandler`, and that handler is added to the relevant logger (or root logger) using `basicConfig` or `dictConfig`. If using `dictConfig`, explicitly set `"disable_existing_loggers": false` if you need existing loggers to continue logging.
This is the designed behavior since v0.0.4. If multi-line log statements are desired (which goes against the logfmt philosophy), either downgrade `logfmter` to a version prior to 0.0.4 (not recommended, as it fixes a bug) or adjust logging to avoid embedding newlines.
Install the package using pip: `pip install logfmter`.
Pass the `datefmt` parameter directly to the `Logfmter` constructor, e.g., `Logfmter(datefmt="%Y-%m-%d %H:%M:%S")`.
No dependency data recorded yet.