logfmt logging for Python
Project description
logformat - logfmt logging for Python
- implements the same value escaping as the logfmt library used by Grafana Loki
- implements a
logging.Formatterso it works with any library using the standard library logging - implements a convenience wrapper around
logging.Loggingfor specifying structured fields viakwargs
import logging
import logformat
logfmt_handler = logging.StreamHandler()
logfmt_handler.setFormatter(logformat.LogfmtFormatter())
logging.basicConfig(handlers=[logfmt_handler], level=logging.INFO)
logger = logformat.get_logger()
logger.warning("this seems off", some_id=33)
Will output a line like:
time=2025-03-23T06:21:36Z level=warning msg="this seems off" some_id=33
exc_info and stack_info are supported via backtrace=True and stack=True
(which will add same-named fields).
In the main method of a script you should always catch any exception and log
them as errors, so that unexpected errors show up properly in your log file.
This library makes this easy by providing a log_uncaught decorator:
logger = logformat.get_logger()
@logger.log_uncaught
def run():
# your script
...
This library intentionally doesn't provide any pretty terminal output but the produced logs can be viewed with the logfmt CLI (since that tool again uses the logfmt library that this library is compatible with).
Alternatives
-
structlog's
get_loggerreturnsAny. While there's a typed alternative that's a bit of a footgun. -
logfmter doesn't include the time by default, names the time field
whenby default and the level fieldatby default, doesn't use ISO dates by default and isn't compatible with the Go logfmt library as of March 2025
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file logformat-0.1.0.tar.gz.
File metadata
- Download URL: logformat-0.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a28b8698f1f1bcdc4693430ec58ff085a61450023422d9321318781066757d2
|
|
| MD5 |
93714ab58009b17206016a6336431a61
|
|
| BLAKE2b-256 |
31a146fb32395134df424f314edcaaea98a8b287c80b8b73e21995b8f67abfa9
|
File details
Details for the file logformat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: logformat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5ce1962799f05befedfbec796885d5408d949f3c10b06194e1956d465ca2988
|
|
| MD5 |
66a2c7d83dde00afe2f781e169df4a20
|
|
| BLAKE2b-256 |
ab7d1668dbcc88fdb91c370eca5bff531e2108e4b3398c1da561f8cb908e704a
|