Skip to main content

Structured stdlib logging with context vars

Project description

Everything you need to turn Python stdlib logging into a proper structured logging library. Rather than fighting logging, let’s live with it.

import logging
import logstruct

logging.basicConfig(level=logging.INFO)
logging.root.handlers[0].setFormatter(logstruct.StructuredFormatter())

log = logstruct.getLogger(__name__)
log.info("Message with data", key1="val1", data=object())

This prints the following JSON to stderr that, if pretty-printed, looks as follows:

{
  "time": "2025-01-19 09:49:36,489",
  "logger": "__main__",
  "level": "INFO",
  "func": "<module>",
  "line": 8,
  "message": "Message with data",
  "key1": "val1",
  "data": "<object object at 0x765a8a9806f0>"
}

Full documentation

Features

  • StructuredLogger <logstruct.StructuredLogger> - a replacement for logging.Logger with a simplified interface for adding structured data to logs. (Usage <logger_usage>)

  • StructuredFormatter <logstruct.StructuredFormatter> - a logging.Formatter implementation that formats log records as JSON. (Usage <formatter_usage>)

  • contextual information <context_usage> to log records with logstruct.context_scope.

  • Easy to use and simple to configure, no dependencies.

  • Seamlessly integrates with any code using stdlib logging, e.g. Sentry SDK.

  • Human readable output for development - see demo_dev_mode.py <dev_mode_logging>.

Design principles

  1. Play well with logging.

  2. Be small.

Considerations

If the standard logging library adds a new kwarg to log methods, e.g. logging.Logger.info, this kwarg, when passed to StructuredLogger, will be merged into the extra dict until it is added to StructuredLogger methods. Using StructuredLogger is optional.

Logging integrations that rely on monkey-patching logging.Formatter.format won’t see it called because StructuredFormatter doesn’t call this method. Such reliance is extremely unlikely.

Development

While the project source is dependency-free, PDM is used for management of dev (testing) and doc (Sphinx/ReadTheDocs) dependencies.

pdm install

You should be able to get away with not using PDM as long as you don’t change dependencies.

pip install --editable . -r requirements-dev.txt -r requirements-doc.txt

When dependencies are changed, they need to be locked. This will also write requirements-{dev,doc}.txt.

pdm lock -G :all

Tests are run with pytest and Sphinx’s doctest target.

pdm run pytest
pdm run sphinx-build docs docs/_build -b doctest

Setuptools-SCM and build are used for building the project. Publishing is done in the CI, using the old twine method, even though PDM could be used.

British English is used in the project, out of fear of losing my settled status.

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

logstruct-0.2.1.tar.gz (54.6 kB view details)

Uploaded Source

Built Distribution

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

logstruct-0.2.1-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file logstruct-0.2.1.tar.gz.

File metadata

  • Download URL: logstruct-0.2.1.tar.gz
  • Upload date:
  • Size: 54.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for logstruct-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2ac69748ec2e32dca24bc72c902726517071286ab35b0614d26915279da35c6c
MD5 a32ad233386df068cf2a4a029f713740
BLAKE2b-256 ac1d0242a446f1ff64f42e23f619215c0a3c34f9c8dd76b2761fb97979a18175

See more details on using hashes here.

File details

Details for the file logstruct-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: logstruct-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for logstruct-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f35a913d11267a2a274f6ffb5934c411de31f54c8af7581e16f588b20b345820
MD5 8d10d21caaec28e7d87401edd7f20993
BLAKE2b-256 a3848ef2518ebe9cb0f999317b77694405407d3954ac7447650a6a317542bb55

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