opentelemetry._logs package

Module contents

The OpenTelemetry logging API describes the classes used to generate logs and events.

The LoggerProvider provides users access to the Logger which in turn is used to create Event and Log objects.

This module provides abstract (i.e. unimplemented) classes required for logging, and a concrete no-op implementation NoOpLogger that allows applications to use the API package alone without a supporting implementation.

To get a logger, you need to provide the package name from which you are calling the logging APIs to OpenTelemetry by calling LoggerProvider.get_logger with the calling module name and the version of your package.

The following code shows how to obtain a logger using the global LoggerProvider:

from opentelemetry._logs import get_logger

logger = get_logger("example-logger")

New in version 1.15.0.

class opentelemetry._logs.Logger(name, version=None, schema_url=None)[source]

Bases: abc.ABC

Handles emitting events and logs via LogRecord.

abstract emit(record)[source]

Emits a LogRecord representing a log to the processing pipeline.

Return type

None

class opentelemetry._logs.LoggerProvider[source]

Bases: abc.ABC

LoggerProvider is the entry point of the API. It provides access to Logger instances.

abstract get_logger(name, version=None, schema_url=None)[source]

Returns a Logger for use by the given instrumentation library.

For any two calls it is undefined whether the same or different Logger instances are returned, even for different library names.

This function may return different Logger types (e.g. a no-op logger vs. a functional logger).

Parameters
  • name (str) –

    The name of the instrumenting module. __name__ may not be used as this can result in different logger names if the loggers are in different files. It is better to use a fixed string that can be imported where needed and used consistently as the name of the logger.

    This should not be the name of the module that is instrumented but the name of the module doing the instrumentation. E.g., instead of "requests", use "opentelemetry.instrumentation.requests".

  • version (Optional[str]) – Optional. The version string of the instrumenting library. Usually this should be the same as importlib.metadata.version(instrumenting_library_name).

  • schema_url (Optional[str]) – Optional. Specifies the Schema URL of the emitted telemetry.

Return type

Logger

class opentelemetry._logs.LogRecord(timestamp=None, observed_timestamp=None, trace_id=None, span_id=None, trace_flags=None, severity_text=None, severity_number=None, body=None, attributes=None)[source]

Bases: abc.ABC

A LogRecord instance represents an event being logged.

LogRecord instances are created and emitted via Logger every time something is logged. They contain all the information pertinent to the event being logged.

class opentelemetry._logs.NoOpLogger(name, version=None, schema_url=None)[source]

Bases: opentelemetry._logs._internal.Logger

The default Logger used when no Logger implementation is available.

All operations are no-op.

emit(record)[source]

Emits a LogRecord representing a log to the processing pipeline.

Return type

None

class opentelemetry._logs.NoOpLoggerProvider[source]

Bases: opentelemetry._logs._internal.LoggerProvider

The default LoggerProvider used when no LoggerProvider implementation is available.

get_logger(name, version=None, schema_url=None)[source]

Returns a NoOpLogger.

Return type

Logger

opentelemetry._logs.get_logger(instrumenting_module_name, instrumenting_library_version='', logger_provider=None, schema_url=None)[source]

Returns a Logger for use within a python process.

This function is a convenience wrapper for opentelemetry.sdk._logs.LoggerProvider.get_logger.

If logger_provider param is omitted the current configured one is used.

Return type

Logger

opentelemetry._logs.get_logger_provider()[source]

Gets the current global LoggerProvider object.

Return type

LoggerProvider

opentelemetry._logs.set_logger_provider(meter_provider)[source]

Sets the current global LoggerProvider object.

This can only be done once, a warning will be logged if any further attempt is made.

Return type

None

class opentelemetry._logs.SeverityNumber(value)[source]

Bases: enum.Enum

Numerical value of severity.

Smaller numerical values correspond to less severe events (such as debug events), larger numerical values correspond to more severe events (such as errors and critical events).

See the Log Data Model spec for more info and how to map the severity from source format to OTLP Model.

UNSPECIFIED = 0
TRACE = 1
TRACE2 = 2
TRACE3 = 3
TRACE4 = 4
DEBUG = 5
DEBUG2 = 6
DEBUG3 = 7
DEBUG4 = 8
INFO = 9
INFO2 = 10
INFO3 = 11
INFO4 = 12
WARN = 13
WARN2 = 14
WARN3 = 15
WARN4 = 16
ERROR = 17
ERROR2 = 18
ERROR3 = 19
ERROR4 = 20
FATAL = 21
FATAL2 = 22
FATAL3 = 23
FATAL4 = 24
opentelemetry._logs.std_to_otel(levelno)[source]

Map python log levelno as defined in https://docs.python.org/3/library/logging.html#logging-levels to OTel log severity number as defined here: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/data-model.md#field-severitynumber

Return type

SeverityNumber