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, cnpj, cpfCnpj, documento,
numeroDocumento, taxId, apiKey, clientSecret, senha, password,
token, authorization, cartao/cartão, chavePix and related credential
fields are redacted by default. In messages, the library redacts only the
sensitive fragment/value and preserves the rest of the log. In structured
context, sensitive keys redact the full value. The key matcher normalizes
common snake_case, camelCase, kebab-case, dotted variants, and accents.
Set LOGS_FILTER_SANITIZE=false, LOGS_SANITIZE=false, or
UO_LOGS_SANITIZE=false only when raw sensitive data is explicitly required
and approved.
Environment Variables
The package supports the Python LOGS_* configuration surface aligned with
the JS v3 design where applicable.
Required for direct Loki mode:
LOGS_URLLOGS_TENANTLOGS_APP_NAME
For collector-first mode, set LOGS_EXPORTER=otlp (or collector) and use
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT. If the explicit logs endpoint is missing,
the library derives /v1/logs from OTEL_EXPORTER_OTLP_TRACES_ENDPOINT,
OTEL_EXPORTER_OTLP_METRICS_ENDPOINT, or OTEL_EXPORTER_OTLP_ENDPOINT.
In this mode LOGS_URL, LOKI_URL, LOGS_TENANT and LOGS_TOKEN are not
required by the library. LOGS_URL is intentionally ignored in collector mode
to avoid accidentally posting OTLP logs to a direct Loki /loki/api/v1/push
endpoint. Put tenant or auth headers in
OTEL_EXPORTER_OTLP_HEADERS/LOGS_OTLP_HEADERS if your collector gateway
requires them.
Core:
LOGS_TOKENLOGS_APP_VERSIONLOGS_ENVIRONMENT
Transport:
LOGS_EXPORTER(loki|otlp, aliases:collector,otel)OTEL_EXPORTER_OTLP_LOGS_ENDPOINT(collector logs endpoint, e.g.http://collector:4318/v1/logs)LOGS_OTLP_HEADERS(comma-separated headers, e.g.x-tenant=team-a)LOGS_COMPRESSION(none|gzip|brotli|snappy)LOGS_COMPRESSION_LEVELLOGS_COMPRESSION_THRESHOLDLOGS_USE_WORKERSLOGS_MAX_WORKERSLOGS_WORKER_TIMEOUTLOGS_CONNECTION_POOLINGLOGS_MAX_SOCKETSLOGS_ENABLE_STRUCTURED_METADATA(falseby default; enable only when Loki allowslimits_config.allow_structured_metadata)LOGS_TIMEOUTLOGS_MAX_RETRIESLOGS_RETRY_DELAY
Buffer:
LOGS_BUFFER_MAX_SIZELOGS_BUFFER_FLUSH_INTERVALLOGS_BUFFER_MAX_MEMORY_MBLOGS_BUFFER_MAX_AGELOGS_BUFFER_AUTO_FLUSH
Filter:
LOGS_FILTER_LEVELSLOGS_FILTER_SAMPLING_RATELOGS_FILTER_SANITIZE(aliases:LOGS_SANITIZE,UO_LOGS_SANITIZE)LOGS_FILTER_MAX_MESSAGE_LENGTHLOGS_FILTER_EXCLUDE_LOGGER_PREFIXES(comma-separated)
Circuit Breaker:
LOGS_CIRCUIT_BREAKER_ENABLEDLOGS_CIRCUIT_BREAKER_FAILURE_THRESHOLDLOGS_CIRCUIT_BREAKER_RESET_TIMEOUTLOGS_CIRCUIT_BREAKER_HALF_OPEN_REQUESTS
DLQ:
LOGS_DLQ_ENABLEDLOGS_DLQ_TYPE(memory|file)LOGS_DLQ_MAX_SIZELOGS_DLQ_MAX_RETRIESLOGS_DLQ_BASE_PATH
Runtime:
LOGS_MAX_CONCURRENT_FLUSHESLOGS_INTERCEPT_CONSOLELOGS_PRESERVE_ORIGINAL_CONSOLELOGS_ENABLE_METRICSLOGS_ENABLE_HEALTH_CHECKLOGS_DEBUGLOGS_SILENT_ERRORSLOGS_ENABLEDLOGS_AUTO_INITLOGS_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()loggerproxy withdebug/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_TOKENfor--repository testpypiPYPI_API_TOKENfor--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_TOKENPYPI_API_TOKEN
License
MIT
Metadata
Release files for elven-logs-interceptor-python 0.1.17
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| elven_logs_interceptor_python-0.1.17.tar.gz | 43.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| elven_logs_interceptor_python-0.1.17-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 111.0 kB
Release files / elven_logs_interceptor_python-0.1.17.tar.gz
| Download URL | elven_logs_interceptor_python-0.1.17.tar.gz |
|---|---|
| Size | 43.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9651dbf5121790479c706be0da47ac9147fa8e88a7079afcb16f6558391825da
|
|
BLAKE2b-256 checksum How to use checksums |
547ae63952f940dffe30bfb97d35400fc41fc9fb0155bf00071207c02993c6cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.16
|
Release files / elven_logs_interceptor_python-0.1.17-py3-none-any.whl
| Download URL | elven_logs_interceptor_python-0.1.17-py3-none-any.whl |
|---|---|
| Size | 67.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
16adfb8b30343122b2fe9234f2d5f2e5887c620c1d633c9ffdd4bc3c8487d2db
|
|
BLAKE2b-256 checksum How to use checksums |
b79428e30566586d756ffac27c9c4e61a1364dffb4db095b4b3737ebb3a21770
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.16
|