or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

attributes.mdbridges.mdcontext-propagation.mdexporters-other.mdexporters-otlp.mdindex.mdlogging.mdmetrics.mdsdk-log.mdsdk-metric.mdsdk-resource.mdsdk-trace.mdsemantic-conventions.mdtracing.md
tile.json

logging.mddocs/

Logging API (Beta)

The OpenTelemetry Go logging API provides interfaces for emitting structured logs integrated with traces and metrics. Note: The logging API is currently in Beta status.

Package Import

import "go.opentelemetry.io/otel/log"

Overview

The logging API enables:

  • Structured log records with attributes
  • Integration with trace context
  • Severity levels aligned with OpenTelemetry specification
  • Bridge support for existing logging libraries

Quick Start

import (
	"context"
	"go.opentelemetry.io/otel/log"
	"go.opentelemetry.io/otel/log/global"
)

func main() {
	// Get logger
	logger := global.GetLoggerProvider().Logger("my-service")

	// Create log record
	var record log.Record
	record.SetTimestamp(time.Now())
	record.SetObservedTimestamp(time.Now())
	record.SetSeverity(log.SeverityInfo)
	record.SetSeverityText("INFO")
	record.SetBody(log.StringValue("User logged in"))
	record.AddAttributes(
		log.String("user.id", "12345"),
		log.String("session.id", "abc-123"),
	)

	// Emit log
	logger.Emit(context.Background(), record)
}

Core Interfaces

LoggerProvider

type LoggerProvider interface {
	// Logger returns a new Logger with the provided name and configuration.
	Logger(name string, options ...LoggerOption) Logger
}

Logger

type Logger interface {
	// Emit emits a log record.
	Emit(ctx context.Context, record Record)

	// Enabled reports whether the Logger emits for the given context and param.
	Enabled(ctx context.Context, param EnabledParameters) bool
}

LoggerOption

Configuration options for creating a Logger.

type LoggerOption interface {
	// Has unexported methods.
}

// WithInstrumentationVersion sets the instrumentation version for the Logger.
func WithInstrumentationVersion(version string) LoggerOption

// WithSchemaURL sets the schema URL for the Logger.
func WithSchemaURL(schemaURL string) LoggerOption

// WithInstrumentationAttributes sets the instrumentation attributes for the Logger.
// The passed attributes will be de-duplicated.
func WithInstrumentationAttributes(attr ...attribute.KeyValue) LoggerOption

Example:

logger := provider.Logger(
	"github.com/myorg/myapp",
	log.WithInstrumentationVersion("1.0.0"),
	log.WithSchemaURL("https://opentelemetry.io/schemas/1.20.0"),
	log.WithInstrumentationAttributes(
		attribute.String("environment", "production"),
	),
)

Record

type Record struct {
	// Has unexported fields.
}

// Setters
func (r *Record) SetTimestamp(t time.Time)
func (r *Record) SetObservedTimestamp(t time.Time)
func (r *Record) SetSeverity(level Severity)
func (r *Record) SetSeverityText(text string)
func (r *Record) SetBody(v Value)
func (r *Record) AddAttributes(attrs ...KeyValue)
func (r *Record) SetEventName(s string)

// Getters
func (r *Record) Timestamp() time.Time
func (r *Record) ObservedTimestamp() time.Time
func (r *Record) Severity() Severity
func (r *Record) SeverityText() string
func (r *Record) Body() Value
func (r *Record) EventName() string
func (r *Record) AttributesLen() int
func (r *Record) WalkAttributes(f func(KeyValue) bool)

// Utility
func (r *Record) Clone() Record

Severity Levels

type Severity int

const (
	SeverityUndefined Severity = 0

	SeverityTrace1 Severity = 1  // TRACE
	SeverityTrace2 Severity = 2
	SeverityTrace3 Severity = 3
	SeverityTrace4 Severity = 4

	SeverityDebug1 Severity = 5  // DEBUG
	SeverityDebug2 Severity = 6
	SeverityDebug3 Severity = 7
	SeverityDebug4 Severity = 8

	SeverityInfo1 Severity = 9   // INFO
	SeverityInfo2 Severity = 10
	SeverityInfo3 Severity = 11
	SeverityInfo4 Severity = 12

	SeverityWarn1 Severity = 13  // WARN
	SeverityWarn2 Severity = 14
	SeverityWarn3 Severity = 15
	SeverityWarn4 Severity = 16

	SeverityError1 Severity = 17 // ERROR
	SeverityError2 Severity = 18
	SeverityError3 Severity = 19
	SeverityError4 Severity = 20

	SeverityFatal1 Severity = 21 // FATAL
	SeverityFatal2 Severity = 22
	SeverityFatal3 Severity = 23
	SeverityFatal4 Severity = 24

	// Convenience aliases
	SeverityTrace = SeverityTrace1
	SeverityDebug = SeverityDebug1
	SeverityInfo  = SeverityInfo1
	SeverityWarn  = SeverityWarn1
	SeverityError = SeverityError1
	SeverityFatal = SeverityFatal1
)

Values and KeyValues

Value Types

type Value struct {
	// Has unexported fields.
}

// Constructors
func BoolValue(v bool) Value
func Int64Value(v int64) Value
func IntValue(v int) Value
func Float64Value(v float64) Value
func StringValue(v string) Value
func BytesValue(v []byte) Value
func SliceValue(vs ...Value) Value
func MapValue(kvs ...KeyValue) Value

// Conversion from attribute package
func ValueFromAttribute(value attribute.Value) Value

// Getters
func (v Value) AsBool() bool
func (v Value) AsInt64() int64
func (v Value) AsFloat64() float64
func (v Value) AsString() string
func (v Value) AsBytes() []byte
func (v Value) AsSlice() []Value
func (v Value) AsMap() []KeyValue

// Utility
func (v Value) Kind() Kind
func (v Value) Empty() bool
func (v Value) Equal(w Value) bool
func (v Value) String() string

KeyValue Pairs

type KeyValue struct {
	Key   string
	Value Value
}

// Constructors
func Bool(key string, value bool) KeyValue
func Int(key string, value int) KeyValue
func Int64(key string, value int64) KeyValue
func Float64(key string, value float64) KeyValue
func String(key, value string) KeyValue
func Bytes(key string, value []byte) KeyValue
func Slice(key string, value ...Value) KeyValue
func Map(key string, value ...KeyValue) KeyValue
func Empty(key string) KeyValue

// Conversion from attribute package
func KeyValueFromAttribute(kv attribute.KeyValue) KeyValue

// Utility
func (a KeyValue) Equal(b KeyValue) bool
func (a KeyValue) String() string

Example: Converting from attribute package:

import (
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/log"
)

// Convert individual attributes
attrKV := attribute.String("key", "value")
logKV := log.KeyValueFromAttribute(attrKV)

// Convert attribute values
attrValue := attribute.IntValue(42)
logValue := log.ValueFromAttribute(attrValue)

// Use in log record
var record log.Record
record.AddAttributes(logKV)
record.SetBody(logValue)

Complete Example

package main

import (
	"context"
	"time"

	"go.opentelemetry.io/otel/log"
	sdklog "go.opentelemetry.io/otel/sdk/log"
	"go.opentelemetry.io/otel/log/global"
)

func main() {
	// Setup logger provider
	processor := sdklog.NewBatchProcessor(exporter)
	provider := sdklog.NewLoggerProvider(
		sdklog.WithProcessor(processor),
	)
	global.SetLoggerProvider(provider)
	defer provider.Shutdown(context.Background())

	// Get logger
	logger := provider.Logger("my-service",
		log.WithInstrumentationVersion("1.0.0"),
	)

	// Create and emit log record
	var record log.Record
	record.SetTimestamp(time.Now())
	record.SetObservedTimestamp(time.Now())
	record.SetSeverity(log.SeverityInfo)
	record.SetSeverityText("INFO")
	record.SetBody(log.StringValue("Processing request"))
	record.AddAttributes(
		log.String("user.id", "12345"),
		log.Int("request.size", 1024),
		log.Float64("duration", 0.123),
	)

	logger.Emit(context.Background(), record)
}

Related Documentation

  • SDK Log: SDK implementation for logging
  • Tracing: Integration with distributed tracing