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

Uploaded Python 3

File details

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

File metadata

  • Download URL: otel_log_proto_mapper-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 048503630b6211bc84bf2d62eaa918cf0c7a64cc4ff1e49430cb3bfc9b154360
MD5 34a8a4eaafb6e755b2491f575279aa44
BLAKE2b-256 a02775c272fc7c10b961576434453ebf21ca83aee837be4003ff6e726e27ce3d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for otel_log_proto_mapper-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 084c3b7c5e388a82d6355fe1f0b1636beb99c29d399af1b3f23130152a8cd7d3
MD5 ab31510ce8e33bb3d4ac3c71382cd6b6
BLAKE2b-256 f4caf37c3fdd1b93713845dc2af691e807bd45526488721ab30fc0bb11e7dc04

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