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-1.1.1.tar.gz (4.0 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-1.1.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file log-enricher-1.1.1.tar.gz.

File metadata

  • Download URL: log-enricher-1.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

File hashes

Hashes for log-enricher-1.1.1.tar.gz
Algorithm Hash digest
SHA256 af5308a873dc6688a291ec6a9bd3f5646472cac7a195e4577c11c12e5734e531
MD5 e8be873d929ffe133b7b78fd8d6ae107
BLAKE2b-256 1144dafa660000d2350627405bf46505b4ebe31da1c8422f8a4521b173f919c4

See more details on using hashes here.

File details

Details for the file log_enricher-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: log_enricher-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Darwin/19.6.0

File hashes

Hashes for log_enricher-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 37ddf3bc6b8619833df23cea5c161a44a91b0f052410129d3d47ff2e9e74c8e8
MD5 12a4ef5062c650bb97f697add541adba
BLAKE2b-256 60bd8c72f15f8de6853a8d591e209be102e4b41e5d6c78d1c2880fcffe76a123

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