Skip to main content

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.LogRecord attributes (message, level, extra data, exception info) into OTLP Log Protobuf fields.
  • Supports mapping of common Python types to OTLP AnyValue.
  • Provides an OTEL_SEVERITY_MAP for 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

otel_log_proto_mapper-0.1.2.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

otel_log_proto_mapper-0.1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file otel_log_proto_mapper-0.1.2.tar.gz.

File metadata

  • Download URL: otel_log_proto_mapper-0.1.2.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

Hashes for otel_log_proto_mapper-0.1.2.tar.gz
Algorithm Hash digest
SHA256 679a87cbde8aa9e7f02995a52b57376236a92d658d1875c6d2553b828b323bf5
MD5 7796508f7665f6e2c8f2b689d682d24b
BLAKE2b-256 c09d2ab8326e4bb259a48b16bf63c89e2622f69f3eac91809e989917b0214d32

See more details on using hashes here.

File details

Details for the file otel_log_proto_mapper-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for otel_log_proto_mapper-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a127098794348f08ad810733e33011c2024a23afdfe0973f284779d9896a3a4b
MD5 fda33c6dd67b2800dbc7802f978f971d
BLAKE2b-256 fdd92f79f8c45398c1af8aa14c4a3e1a898fb1e52f2f690ecb58d26bc8e408b4

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