Skip to main content

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.

Metadata

Release files for log-enricher 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for log-enricher 2.1.1
File Size Uploaded Provenance Actions
log_enricher-2.1.1.tar.gz 3.6 kB Without provenance Details for log_enricher-2.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for log-enricher 2.1.1
File Interpreter ABI Platform Actions
log_enricher-2.1.1-py3-none-any.whl Python 3 none any Details for log_enricher-2.1.1-py3-none-any.whl

Total release size: 7.7 kB

Release files / log_enricher-2.1.1.tar.gz

Download URL log_enricher-2.1.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
87377599238796dcf11cd80bb95ccd7e80dc9e55d24ad9b85433fdb052bdb5dc
BLAKE2b-256 checksum
How to use checksums
7ea28ecd4bf822044d0c02125d2b79b7775eb2719a3fb0c9e4e854b7a1b070d9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.5 Darwin/24.5.0

Release files / log_enricher-2.1.1-py3-none-any.whl

Download URL log_enricher-2.1.1-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2720a29bcec2491a17f951c318c7a5dd3f1fb6e6a1ecf1ac29ae009eb969689b
BLAKE2b-256 checksum
How to use checksums
5bb21dd029fd4712d13a7dd45852e6aba4dfdb16acf2407deebd3bfeb7adc0e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.11.5 Darwin/24.5.0

Security for log-enricher 2.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page