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.3.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.3-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: otel_log_proto_mapper-0.1.3.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.3.tar.gz
Algorithm Hash digest
SHA256 d6957a27b440c300887c453f1a543b58ce66dc9178a49841d12817481bfb08b5
MD5 ccbafdf31433d771e856f95fc56604d1
BLAKE2b-256 612c7de2f90843b2106ed7a2b1669d299abd9d77047308465ff6362453729c3b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otel_log_proto_mapper-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 28e766baf68f255d1b4c0b75d0d05b64bf9c2fca64fdba9b3a03ef33897f44f9
MD5 373b784f7e6db700ea71ba8ae3967239
BLAKE2b-256 f1da78e4ef2f820e7ba809737ecd186e4a1f07d12855cdf7a0993ee3c854cc2a

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