Skip to main content

Library to enrich structured logs

Project description

log-enricher

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.

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


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.1.1.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

log_enricher-2.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

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

Hashes for log_enricher-2.1.1.tar.gz
Algorithm Hash digest
SHA256 87377599238796dcf11cd80bb95ccd7e80dc9e55d24ad9b85433fdb052bdb5dc
MD5 3f9f2e1dc52f7d283346d5db1ef9396d
BLAKE2b-256 7ea28ecd4bf822044d0c02125d2b79b7775eb2719a3fb0c9e4e854b7a1b070d9

See more details on using hashes here.

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

Hashes for log_enricher-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2720a29bcec2491a17f951c318c7a5dd3f1fb6e6a1ecf1ac29ae009eb969689b
MD5 9b270e77ebeb36b3196205fbd8305ae7
BLAKE2b-256 5bb21dd029fd4712d13a7dd45852e6aba4dfdb16acf2407deebd3bfeb7adc0e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page