An opinionated hydra plugin to configure logging with structlog
Project description
Hydra-Structlog
An opinionated hydra plugin to configure logging with structlog.
Usage
Structlog is fully integrated in Hydra through a config group named structlog for hydra.job_logging.
-
Install the plugin.
uv add hydra-structlog
-
Override the job logging with a default in your config.
defaults: - override hydra/job_logging=structlog
-
Hydra will now emit JSON logs (by default to
structured.log) and print human readably logs to the console.
Customization
Structlog comes configured with opinionated defaults.
To adjust the processors, use the hydra defaults as outlined above and overwrite individual config entries afterwards. For example:
defaults:
- override hydra/job_logging=structlog
# Change the storage location
hydra:
job_logging:
handlers:
json:
filename: ${hydra.runtime.cwd}/logs/structured.log
Defaults
The configuration runs structlog through the standard library logging. That has several advantages.
The use of structlog and logging, e.g. by third-party packages, are recorded and both exhibit structlog's bound contextvars.
The processor chain is conveniently exposed in the job_logging configuration and adjustable through hydra's config interface.
Because lists are not easily manipulated in hydra configs, the processor chains are defined as dictionaries. The name of each processor does not matter as long as its unique in the chain. The default processor chain can then be extended more comfortably.
To remove a processor set it to null. See example below.
Because structlog's ProcessorFormatter does not accept the processors as strings, the logging config contains _target_ instantiation.
defaults:
- override hydra/job_logging=structlog
# Remove a processor from the default chain and add another one
hydra:
job_logging:
formatters:
json:
foreign_pre_chain:
# Remove the add-log-level processor
add_logger_name: null
# Append a new processor
arbitrary_name:
# Follow hydra's instantiate syntax
_target_: structlog.stdlib.add_log_level_number
_partial_: true
Notes
Python 3.14 is currently not supported as hydra.main causes a ValueError on argparse-validation, see https://github.com/facebookresearch/hydra/issues/3121.
It is recommended to use hydra-zen which does not have that problem and works well with Python 3.14.
Tracer injection is not implemented here as data science projects using hydra typically do not implement web apps. Nevertheless, an appropriate structlog processor can be added to the job_logging config.
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 hydra_structlog-0.1.0.tar.gz.
File metadata
- Download URL: hydra_structlog-0.1.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fa45a22d57499cd265d72e067ba20b3a17a99bd8cda8e343dd5c84e7abd2fdbc
|
|
| MD5 |
d8c20f7c483565338cd066715462974b
|
|
| BLAKE2b-256 |
7dd609d643f53cd2c177de0238fed19979db6d5ceb821f0aca7b158835abbe0d
|
Provenance
The following attestation bundles were made for hydra_structlog-0.1.0.tar.gz:
Publisher:
pypi.yml on szapp/hydra-structlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hydra_structlog-0.1.0.tar.gz -
Subject digest:
fa45a22d57499cd265d72e067ba20b3a17a99bd8cda8e343dd5c84e7abd2fdbc - Sigstore transparency entry: 1194965810
- Sigstore integration time:
-
Permalink:
szapp/hydra-structlog@d69825652ac008a5e5366199d0f3fab24040ebe5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/szapp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d69825652ac008a5e5366199d0f3fab24040ebe5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file hydra_structlog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hydra_structlog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4324782af9dae88ea6f969ba701cfad5f64b275b97897ec14dc81d28119aeec7
|
|
| MD5 |
53f4e98cec1d5acfb1cde20a48295006
|
|
| BLAKE2b-256 |
b094a43510bc473777277a47e85aeca6ee83297cd33648d0d2de505b895fff3a
|
Provenance
The following attestation bundles were made for hydra_structlog-0.1.0-py3-none-any.whl:
Publisher:
pypi.yml on szapp/hydra-structlog
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hydra_structlog-0.1.0-py3-none-any.whl -
Subject digest:
4324782af9dae88ea6f969ba701cfad5f64b275b97897ec14dc81d28119aeec7 - Sigstore transparency entry: 1194965859
- Sigstore integration time:
-
Permalink:
szapp/hydra-structlog@d69825652ac008a5e5366199d0f3fab24040ebe5 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/szapp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@d69825652ac008a5e5366199d0f3fab24040ebe5 -
Trigger Event:
push
-
Statement type: