OpenTelemetry log handler plugin for simple_log_factory
Project description
simple-log-factory-ext-otel
OpenTelemetry log handler plugin for simple_log_factory.
Ship your log messages to any OpenTelemetry-compatible backend (Tempo, Jaeger, Grafana Cloud, SigNoz, etc.) without changing any existing logging code.
Installation
pip install simple-log-factory-ext-otel
Or with UV:
uv add simple-log-factory-ext-otel
Quick Start
from simple_log_factory import log_factory
from simple_log_factory_ext_otel import OtelLogHandler
# 1. Initialize the handler
otel_handler = OtelLogHandler(
service_name="my-service",
endpoint="http://localhost:4317",
)
# 2. Pass it to log_factory — done
logger = log_factory(
__name__,
custom_handlers=[otel_handler],
)
logger.info("This goes to console AND to your OTel backend")
# 3. Clean shutdown (optional but recommended)
otel_handler.shutdown()
Configuration
OtelLogHandler Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
service_name |
str |
(required) | Logical name of the service emitting logs |
endpoint |
str |
http://localhost:4317 |
OTLP receiver endpoint |
protocol |
str |
"grpc" |
Transport protocol — "grpc" or "http" |
insecure |
bool |
True |
Use plaintext (insecure) connection |
headers |
Dict[str, str] |
None |
Metadata headers sent with every export request |
resource_attributes |
Dict[str, str] |
None |
Extra OTel Resource attributes |
log_level |
int |
logging.NOTSET |
Minimum severity forwarded to the OTel pipeline |
export_timeout_millis |
int |
30000 |
Timeout in ms for each export batch |
Using HTTP Instead of gRPC
from simple_log_factory_ext_otel import OtelLogHandler
handler = OtelLogHandler(
service_name="my-service",
endpoint="http://localhost:4318/v1/logs",
protocol="http",
)
Custom Resource Attributes
from simple_log_factory_ext_otel import OtelLogHandler
handler = OtelLogHandler(
service_name="my-service",
resource_attributes={
"deployment.environment": "production",
"service.version": "1.2.3",
},
)
Authenticated Endpoints
from simple_log_factory_ext_otel import OtelLogHandler
handler = OtelLogHandler(
service_name="my-service",
endpoint="https://otel.example.com:4317",
insecure=False,
headers={"Authorization": "Bearer <token>"},
)
Level Filtering
Only export warnings and above to your OTel backend:
import logging
from simple_log_factory_ext_otel import OtelLogHandler
handler = OtelLogHandler(
service_name="my-service",
log_level=logging.WARNING,
)
How It Works
OtelLogHandler extends logging.Handler directly (not OTel's LoggingHandler) and internally composes the OTel pipeline:
Your code
└─► log_factory (attaches handler, sets formatter & level)
└─► OtelLogHandler.emit(record)
└─► Internal OTel LoggingHandler (LogRecord → OTel translation)
└─► BatchLogRecordProcessor
└─► OTLPLogExporter (gRPC or HTTP)
└─► OTel Collector / Backend
This composition pattern is critical: log_factory calls setFormatter() and setLevel() on every handler it receives. If we inherited from OTel's LoggingHandler, the factory's formatter would overwrite OTel's internal translation. By wrapping it, both pipelines stay independent.
Trace Context Correlation
If you're using OpenTelemetry tracing, span and trace IDs are automatically attached to log records. No extra configuration needed.
Lifecycle Management
The handler registers an atexit hook to flush and shut down on interpreter exit. For explicit control:
# Force-flush buffered records
otel_handler.flush()
# Graceful shutdown (idempotent, safe to call multiple times)
otel_handler.shutdown()
Local Development
Create a virtual environment with UV:
uv venv
uv pip install -e ".[dev]"
Run tests:
pytest --cov=simple_log_factory_ext_otel --cov-report=term-missing
Run linters:
black --check .
isort --check .
ruff check .
mypy simple_log_factory_ext_otel
License
GPL-3.0 — see LICENSE.md for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_log_factory_ext_otel-1.0.0.tar.gz.
File metadata
- Download URL: simple_log_factory_ext_otel-1.0.0.tar.gz
- Upload date:
- Size: 20.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c45afd913984ff65a8bb9b026427177ecba2610a0172a1c926ee98bc0d8d8c5
|
|
| MD5 |
6b8e812edf054768a491ba4bf27a51df
|
|
| BLAKE2b-256 |
12cda502d73e571b4966e603e74b84986be60d58f1dc53cde68020d4ca0d1eef
|
Provenance
The following attestation bundles were made for simple_log_factory_ext_otel-1.0.0.tar.gz:
Publisher:
publish.yml on brenordv/log-factory-package-ext-otel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_log_factory_ext_otel-1.0.0.tar.gz -
Subject digest:
6c45afd913984ff65a8bb9b026427177ecba2610a0172a1c926ee98bc0d8d8c5 - Sigstore transparency entry: 927322503
- Sigstore integration time:
-
Permalink:
brenordv/log-factory-package-ext-otel@76fbf8406f1d5d7a388944df3e3320d678767d1f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/brenordv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@76fbf8406f1d5d7a388944df3e3320d678767d1f -
Trigger Event:
push
-
Statement type:
File details
Details for the file simple_log_factory_ext_otel-1.0.0-py3-none-any.whl.
File metadata
- Download URL: simple_log_factory_ext_otel-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ea976fdc4c0485971fafb04b2c7e69d44f4392b8ee0be0970becb991b9e7ea67
|
|
| MD5 |
27f78489a6b905f1578c798fc0b62e9d
|
|
| BLAKE2b-256 |
47e46bb2f4d237fd3cee7c6c58b3b196628fb7002876f76424244e423566e77f
|
Provenance
The following attestation bundles were made for simple_log_factory_ext_otel-1.0.0-py3-none-any.whl:
Publisher:
publish.yml on brenordv/log-factory-package-ext-otel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
simple_log_factory_ext_otel-1.0.0-py3-none-any.whl -
Subject digest:
ea976fdc4c0485971fafb04b2c7e69d44f4392b8ee0be0970becb991b9e7ea67 - Sigstore transparency entry: 927322505
- Sigstore integration time:
-
Permalink:
brenordv/log-factory-package-ext-otel@76fbf8406f1d5d7a388944df3e3320d678767d1f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/brenordv
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@76fbf8406f1d5d7a388944df3e3320d678767d1f -
Trigger Event:
push
-
Statement type: