Library to enrich structured logs
Project description
log-enricher
This is a log enricher, useful for adding custom fields to log records.
This was developed at GRID for use with our python backend services and intended to emit structured logs.
installation
pip install log-enricher
configuration
log-enricher.initialize_logging(...) configures the logging library and takes in enrichers, a list of
functions that return a dictionary. When a log message is sent, the enrichers are run automatically and their
output is added to the log message, if structured logging is enabled.
Furthermore, initialize_logging() takes a list of loggers to use, a switch to control structured_logs
(JSON logs, default on), and a log_level setting.
Logs will be output in a structured JSON format by default - if structured_logs is True -
or in a plain, console-friendly format if structured_logs is False.
config example
import os
from log_enricher import initialize_logging, Level
from log_enricher.enrichers import Enricher
class UserContextEnricher(Enricher):
def __call__(self) -> Dict[str, Any]:
user_context = get_user_context()
return {"username": user_context.get("username")}
extra_log_properties = {
"app_version": Config.APP_VERSION, "release_stage": Config.RELEASE_STAGE
}
def main():
initialize_logging(
loggers=["uvicorn", "sqlalchemy"],
structured_logs=os.environ.get("STRUCTURED_LOGS", True),
log_level=Level.INFO,
enrichers=[UserContextEnricher(), lambda: extra_log_properties],
)
enrichers
To build a log enricher, make a subclass of Enricher, or Callable, and implement __call__(). Any method returning
a dict can be used to enrich log records. See log_enricher/enrichers.py. The key-value
pairs in the dict are added as attribute-value pairs to the log record. Of course, any method calls in the
enrichers need to work in any subsequent context the logging system is called.
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 log_enricher-2.1.1.tar.gz.
File metadata
- Download URL: log_enricher-2.1.1.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87377599238796dcf11cd80bb95ccd7e80dc9e55d24ad9b85433fdb052bdb5dc
|
|
| MD5 |
3f9f2e1dc52f7d283346d5db1ef9396d
|
|
| BLAKE2b-256 |
7ea28ecd4bf822044d0c02125d2b79b7775eb2719a3fb0c9e4e854b7a1b070d9
|
File details
Details for the file log_enricher-2.1.1-py3-none-any.whl.
File metadata
- Download URL: log_enricher-2.1.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.2 CPython/3.11.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2720a29bcec2491a17f951c318c7a5dd3f1fb6e6a1ecf1ac29ae009eb969689b
|
|
| MD5 |
9b270e77ebeb36b3196205fbd8305ae7
|
|
| BLAKE2b-256 |
5bb21dd029fd4712d13a7dd45852e6aba4dfdb16acf2407deebd3bfeb7adc0e4
|