Structured stdlib logging
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].formatter = logstruct.StructuredFormatter()
log = logstruct.getLogger(__name__)
log.info("An info message with data as kwargs", key1="val1", unrepresentable_data=object())
with logstruct.context_scope(worker_number=1):
log.info("Running job", job_name="something")
Features
Designed to be easy to introduce to existing projects and teams that use stdlib logging. Adding and configuring this library takes a fraction of the time needed to understand structlog.
It will work well with any 3rd party libraries that integrate with logging, e.g. Sentry.
StructuredLogger - a minimal logger shim to merge log call kwargs into the extra dict. You can still use stdlib loggers - the only difference is having to manually pass arbitrary key-value arguments into the extra dict.
StructuredFormatter - a formatter writing log records as JSON or any other format, depending on the config.
Console stdout output mode - see demo_dev_mode.py.
context_scope, add_context, remove_context - task-safe context vars stack.
Design principles
Play well with logging.
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
pip install --editable .[dev]
British English is used in the project, out of fear of losing my settled status.
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
File details
Details for the file logstruct-0.1.tar.gz
.
File metadata
- Download URL: logstruct-0.1.tar.gz
- Upload date:
- Size: 18.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb84357578624e553f3c7ad2743a0d7d598894a0c489ec748d91bd0964d1c06f |
|
MD5 | 3f7bc65ff60ae2c627aa6125179db189 |
|
BLAKE2b-256 | deac0c12b4c9f29452f3c36d5c31895490b5def887c9ff3f535bbb76b042ce3f |
File details
Details for the file logstruct-0.1-py3-none-any.whl
.
File metadata
- Download URL: logstruct-0.1-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d5db4c0a9247baf20dfcf9436014fe9cd1424532164898209ed6dbb2dcb778f1 |
|
MD5 | 8de2db70c5791c87a13122b22594744c |
|
BLAKE2b-256 | 1e00f63bfa16f800f16961b088ecaa72867c5839c2c26da5a37da016e399a9d8 |