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
File details
Details for the file log-enricher-2.1.0.tar.gz
.
File metadata
- Download URL: log-enricher-2.1.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c8d5dfe98e74ceca11491e2c5256b43d0175009f5bc9ed23f0a09b3ced8a9a7 |
|
MD5 | 8a8d11116d951189a19cb79901901dd6 |
|
BLAKE2b-256 | 31ef7395f1f2114d0b6f13e11c21dfc13ab923b33f48268abb167920f89ce3c3 |
File details
Details for the file log_enricher-2.1.0-py3-none-any.whl
.
File metadata
- Download URL: log_enricher-2.1.0-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.12 CPython/3.10.2 Darwin/21.4.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83fb213cf35e3038478790de94cbc71bb8765093aa1658647810e6b6c3c27527 |
|
MD5 | dde6a33c008e629e0a57798a4b555157 |
|
BLAKE2b-256 | 48a46aae393a2f13342cb5b0f56d5d39786ded598be9b02cee11983923fa5f42 |