Skip to main content

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.Formatter so it works with any library using the standard library logging
  • implements a convenience wrapper around logging.Logging for specifying structured fields via kwargs
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_logger returns Any. 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 when by default and the level field at by default, doesn't use ISO dates by default and isn't compatible with the Go logfmt library as of March 2025

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logformat-0.0.1.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logformat-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file logformat-0.0.1.tar.gz.

File metadata

  • Download URL: logformat-0.0.1.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for logformat-0.0.1.tar.gz
Algorithm Hash digest
SHA256 a4d3d0db6f4568f0c06dcd520f196b428eda7b7a50f6059e1e484b74e55cd711
MD5 4b989f501b94c5c2823117540ddbbaa8
BLAKE2b-256 dfa548303ed6d731681c615f2fe5bcdd6a87900398de884d9c8a1ecb02c023b1

See more details on using hashes here.

File details

Details for the file logformat-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: logformat-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for logformat-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a8953c7ff791d488b5dd98d04b197708db58b566164f723ebea1f9d3a8db61c8
MD5 7920631cc196cc7778551b22947414d6
BLAKE2b-256 1877987fb9adfac0cb49548886bd7dbac3951725771d2556e4d6381ce1fa2976

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page