Skip to main content

Production-grade logs interceptor for Python with Loki transport, resilience, batching, and framework integrations.

Project description

elven-logs-interceptor-python

High-performance, production-ready log interceptor for Python with Loki transport, batching, compression, circuit breaker, DLQ, and framework integrations.

Installation

pip install elven-logs-interceptor-python

With all extras:

pip install "elven-logs-interceptor-python[all]"

Quick Start

from logs_interceptor import init, logger

init(
    {
        "appName": "billing-service",
        "interceptConsole": True,
        "transport": {
            "url": "https://loki.example.com/loki/api/v1/push",
            "tenantId": "tenant-a",
            "authToken": "token",
            "compression": "gzip",
        },
    }
)

logger.info("service started", {"port": 3000})

Logging compatibility

The logger accepts both the Elven structured style and common Python logging call patterns. This makes migration safer when legacy code replaces logging.getLogger(__name__) with the Elven proxy.

logger.info("payload created", {"request_id": "req-1"})
logger.info("payload created: %s", payload)
logger.info("payload created: %(id)s", {"id": payload_id})
logger.info("payload created: {}", payload_id)
logger.info("payload created: %s", payload_id, extra={"request_id": "req-1"})
logger.exception("failed to create payload: %s", payload_id)

When intercepting the standard Python logging module, extra={...} is captured under context.extra:

logging.getLogger(__name__).info(
    "payload created",
    extra={"payload": payload},
)

Supported aliases: warning(...), exception(...) and critical(...).

Sensitive fields such as cpf, password, token and authorization are redacted by default. Set LOGS_FILTER_SANITIZE=false only when raw sensitive data is explicitly required and approved.

Environment Variables

The package supports all LOGS_* variables from the JS v3 design.

Required:

  • LOGS_URL
  • LOGS_TENANT
  • LOGS_APP_NAME

Core:

  • LOGS_TOKEN
  • LOGS_APP_VERSION
  • LOGS_ENVIRONMENT

Transport:

  • LOGS_COMPRESSION (none|gzip|brotli|snappy)
  • LOGS_COMPRESSION_LEVEL
  • LOGS_COMPRESSION_THRESHOLD
  • LOGS_USE_WORKERS
  • LOGS_MAX_WORKERS
  • LOGS_WORKER_TIMEOUT
  • LOGS_CONNECTION_POOLING
  • LOGS_MAX_SOCKETS
  • LOGS_TIMEOUT
  • LOGS_MAX_RETRIES
  • LOGS_RETRY_DELAY

Buffer:

  • LOGS_BUFFER_MAX_SIZE
  • LOGS_BUFFER_FLUSH_INTERVAL
  • LOGS_BUFFER_MAX_MEMORY_MB
  • LOGS_BUFFER_MAX_AGE
  • LOGS_BUFFER_AUTO_FLUSH

Filter:

  • LOGS_FILTER_LEVELS
  • LOGS_FILTER_SAMPLING_RATE
  • LOGS_FILTER_SANITIZE
  • LOGS_FILTER_MAX_MESSAGE_LENGTH

Circuit Breaker:

  • LOGS_CIRCUIT_BREAKER_ENABLED
  • LOGS_CIRCUIT_BREAKER_FAILURE_THRESHOLD
  • LOGS_CIRCUIT_BREAKER_RESET_TIMEOUT
  • LOGS_CIRCUIT_BREAKER_HALF_OPEN_REQUESTS

DLQ:

  • LOGS_DLQ_ENABLED
  • LOGS_DLQ_TYPE (memory|file)
  • LOGS_DLQ_MAX_SIZE
  • LOGS_DLQ_MAX_RETRIES
  • LOGS_DLQ_BASE_PATH

Runtime:

  • LOGS_MAX_CONCURRENT_FLUSHES
  • LOGS_INTERCEPT_CONSOLE
  • LOGS_PRESERVE_ORIGINAL_CONSOLE
  • LOGS_ENABLE_METRICS
  • LOGS_ENABLE_HEALTH_CHECK
  • LOGS_DEBUG
  • LOGS_SILENT_ERRORS
  • LOGS_ENABLED
  • LOGS_AUTO_INIT
  • LOGS_ENABLE_EXPERIMENTAL_PROTOBUF (optional, enables experimental snappy/protobuf transport path)

Labels:

  • Prefix LOGS_LABEL_* (example: LOGS_LABEL_SERVICE=billing)

Public API

  • init(config)
  • get_logger()
  • is_initialized()
  • destroy()
  • adestroy()
  • logger proxy with debug/info/warn/warning/error/exception/fatal/critical/log/track_event/flush/aflush/get_metrics/get_health/destroy/adestroy/with_context/with_context_async

Python import remains:

import logs_interceptor

Integrations

  • Python logging (LoggingHandler)
  • FastAPI / Starlette (FastAPIMiddleware)
  • Django (DjangoMiddleware)
  • Flask (FlaskExtension)
  • Celery (CelerySignals)
  • structlog (StructlogProcessor)
  • loguru (LoguruSink)

Auto Init

Set LOGS_AUTO_INIT=true and import the package.

Preload mode:

python -m logs_interceptor.preload

Development

pip install -e ".[dev]"
ruff check .
mypy src
pytest

Deploy / Publish

Local publish script:

./scripts/publish.sh --repository testpypi --dry-run
./scripts/publish.sh --repository testpypi
./scripts/publish.sh --repository pypi

Token environment variables used by default:

  • TEST_PYPI_API_TOKEN for --repository testpypi
  • PYPI_API_TOKEN for --repository pypi

Optional script flags:

  • --skip-checks (skip lint/type/test)
  • --skip-build (skip build/twine check)
  • --token-env CUSTOM_VAR (custom token env var name)
  • --no-skip-existing (upload fails if version already exists)

Makefile shortcuts:

make qa
make publish-dry-run
make publish-testpypi
make publish-pypi

GitHub Actions publish workflow:

  • File: .github/workflows/publish.yml
  • Trigger: manual (workflow_dispatch)
  • Inputs: repository = testpypi | pypi
  • Required secrets:
    • TEST_PYPI_API_TOKEN
    • PYPI_API_TOKEN

License

MIT

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

elven_logs_interceptor_python-0.1.7.tar.gz (44.4 MB view details)

Uploaded Source

Built Distribution

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

elven_logs_interceptor_python-0.1.7-py3-none-any.whl (61.0 kB view details)

Uploaded Python 3

File details

Details for the file elven_logs_interceptor_python-0.1.7.tar.gz.

File metadata

File hashes

Hashes for elven_logs_interceptor_python-0.1.7.tar.gz
Algorithm Hash digest
SHA256 6b12ea52771af01fb54ebde38f17c13722aef0b6f587d8b5d026cce60489fdf6
MD5 1f009c90e15e5b0c7864db544bc6ad96
BLAKE2b-256 2cb5284413bf78413d16cda7bd2311e22201812c3a2001e816924cce56df618b

See more details on using hashes here.

File details

Details for the file elven_logs_interceptor_python-0.1.7-py3-none-any.whl.

File metadata

File hashes

Hashes for elven_logs_interceptor_python-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 668f3c35cc51fd55232244295e2647dc003887f06c810abf6c0f73075bbe6525
MD5 a4a8144d441560b0d431a2bbd20af976
BLAKE2b-256 c97350546c8a86b0fab722f5de07d7f216cb0dcb8349a5ea5f6475bbcded9c1a

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