Skip to main content

Library to enrich structured logs

Project description


CircleCI PyPI Downloads PyPI Version License

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.


pip install log-enricher


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 global log_level setting. For simple configuration, a string with the name of the logger will do. If you want different log levels per logger, pass in a dictionary:

    "name": "mylogger",
    "log_level": "DEBUG"

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():
        loggers=["sqlalchemy", {"name": "uvicorn", "log_level": "DEBUG"}],
        structured_logs=os.environ.get("STRUCTURED_LOGS", True),
        enrichers=[UserContextEnricher(), lambda: extra_log_properties],


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/ 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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

log-enricher-2.0.0.tar.gz (4.3 kB view hashes)

Uploaded source

Built Distribution

log_enricher-2.0.0-py3-none-any.whl (4.3 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page