A Python library to map standard logging.LogRecord messages to OpenTelemetry OTLP Log Protobufs.
Project description
otel-log-proto-mapper
A Python library designed to facilitate the conversion of standard logging.LogRecord objects into OpenTelemetry Protocol (OTLP) Log Protobuf messages. This library provides a decoupled mapping function, enabling flexible integration into custom logging solutions without requiring the full OpenTelemetry SDK.
Features
- Converts
logging.LogRecordattributes (message, level,extradata, exception info) into OTLP Log Protobuf fields. - Supports mapping of common Python types to OTLP
AnyValue. - Provides an
OTEL_SEVERITY_MAPfor standard logging levels.
Installation
pip install otel-log-proto-mapper
Usage
import logging
from otel_log_proto_mapper import convert_log_record, OTEL_SEVERITY_MAP
# Assuming you also have opentelemetry-proto installed for type hints and protobuf classes
from opentelemetry.proto.logs.v1 import logs_pb2 as otel_logs_pb2
# Create a dummy LogRecord
logger = logging.getLogger("my_app")
logger.setLevel(logging.INFO)
record = logging.LogRecord(
name=logger.name,
level=logging.INFO,
pathname="/app/main.py",
lineno=42,
func="do_work",
msg="Processing data",
args=(),
exc_info=None,
created=time.time(),
)
record.user_id = "user123" # Example of an 'extra' attribute
# Convert to OTLP LogRecord
otel_log_record = convert_log_record(record)
print(f"OTLP Severity: {otel_log_record.severity_text} ({otel_log_record.severity_number})")
print(f"OTLP Body: {otel_log_record.body.string_value}")
print("OTLP Attributes:")
for attr in otel_log_record.attributes:
print(f" {attr.key}: {attr.value}")
# Example with exception
try:
raise ValueError("Something went wrong!")
except ValueError:
exc_record = logging.LogRecord(
name=logger.name,
level=logging.ERROR,
pathname="/app/main.py",
lineno=50,
func="handle_error",
msg="An error occurred",
args=(),
exc_info=sys.exc_info(),
created=time.time(),
)
# When using convert_log_record, you typically pass a formatter for full traceback
# like handler.format in the OTLPGrpcLogHandler context.
# For standalone test, you can rely on converter's internal traceback formatting.
otel_exc_log_record = convert_log_record(exc_record)
print("\nOTLP Exception Log:")
for attr in otel_exc_log_record.attributes:
if attr.key.startswith("exception."):
print(f" {attr.key}: {attr.value}")
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
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 otel_log_proto_mapper-0.1.4.tar.gz.
File metadata
- Download URL: otel_log_proto_mapper-0.1.4.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11029a31e2ac7a082514128b2626df2ddf33774dbd19a9404b09beb366fe0769
|
|
| MD5 |
6d64ff7e6962d80633f24b89e079f84a
|
|
| BLAKE2b-256 |
d976c971e470a4f2c110b5d8e89e9f443d0979dadc96ade6c9577a9f2501216c
|
File details
Details for the file otel_log_proto_mapper-0.1.4-py3-none-any.whl.
File metadata
- Download URL: otel_log_proto_mapper-0.1.4-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b3bbfc98954b0588e6bc11db1ca435878404fe3107838f1bdb914ce1d876a25b
|
|
| MD5 |
33aa40b012f112a6d3ed6680799e9975
|
|
| BLAKE2b-256 |
f3b6bfc1bda393e87c7159013e9ebf4b1c1678a4c88749e9a2760fa59b9b558a
|