log
v1.47.0
Documentation
pkg.go.dev ↗Package log provides the OpenTelemetry Logs API.
This API is separate from its implementation, so the instrumentation built from it is reusable. See go.opentelemetry.io/otel/sdk/log for the official OpenTelemetry implementation of this API.
The log package provides the OpenTelemetry Logs API, which serves as a standard interface for generating and managing log records within the OpenTelemetry ecosystem. This package allows users to emit LogRecords, enabling structured, context-rich logging that can be easily integrated with observability tools. It ensures that log data is captured in a way that is consistent with OpenTelemetry's data model.
This package can be used to create bridges between existing logging libraries and OpenTelemetry. Log bridges allow existing logging setups to be integrated with OpenTelemetry. Log bridges can be found in the registry.
API Implementations
This package does not conform to the standard Go versioning policy; all of its interfaces may have methods added to them without a package major version bump. This non-standard API evolution could surprise an uninformed implementation author. They could unknowingly build their implementation in a way that would result in a runtime panic for their users who update to the new API.
The API is designed to help inform an instrumentation author about this non-standard API evolution. It requires them to choose a default behavior for unimplemented interface methods. There are three behavior choices they can make:
- Compilation failure
- Panic
- Default to another implementation
All interfaces in this API embed a corresponding interface from go.opentelemetry.io/otel/log/embedded. If an author wants the default behavior of their implementations to be a compilation failure, signaling to their users that they need to update to the latest version of that implementation, they need to embed the corresponding interface from go.opentelemetry.io/otel/log/embedded in their implementation. For example,
import "go.opentelemetry.io/otel/log/embedded"
type LoggerProvider struct {
embedded.LoggerProvider
// ...
}
If an author wants the default behavior of their implementations to panic, they need to embed the API interface directly.
import "go.opentelemetry.io/otel/log"
type LoggerProvider struct {
log.LoggerProvider
// ...
}
This is not a recommended behavior as it could lead to publishing packages that contain runtime panics when users update other packages that use newer versions of go.opentelemetry.io/otel/log.
Finally, an author can embed another implementation in theirs. The embedded implementation will be used for methods not defined by the author. For example, an author who wants to default to silently dropping the call can use go.opentelemetry.io/otel/log/noop:
import "go.opentelemetry.io/otel/log/noop"
type LoggerProvider struct {
noop.LoggerProvider
// ...
}
It is strongly recommended that authors only embed go.opentelemetry.io/otel/log/noop if they choose this default behavior. That implementation is the only one OpenTelemetry authors can guarantee will fully implement all the API interfaces when a user updates their API.
Types
EnabledParameters
type EnabledParameters struct {
Severity Severity
EventName string
}
EnabledParameters represents the payload for Logger.Enabled.
Logger
type Logger interface {
// Users of the interface can ignore this. This embedded type is only used
// by implementations of this interface. See the "API Implementations"
// section of the package documentation for more information.
embedded.Logger
// Emit emits a log record.
//
// The record may be held by the implementation. Callers should not mutate
// the record after it is passed.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
Emit(ctx context.Context, record Record)
// Enabled reports whether the Logger emits for the given context and
// param.
//
// Calling Enabled is optional. It is not required before calling
// [Logger.Emit].
//
// Enabled is useful when constructing a [Record] is expensive. A caller can
// call Enabled first and skip record construction when it returns false.
// When constructing the Record is inexpensive, a caller can emit it
// directly.
//
// The returned value is not static and may change over time. A cached value
// can become stale.
//
// The passed param is likely to contain only partial information about a
// record (e.g., a param with only the Severity set).
// If a Logger needs more information than is provided, it
// is said to be in an indeterminate state (see below).
//
// The returned value is true if the Logger emits for the provided context
// and param, and false if the Logger does not emit. The returned value may be
// true or false in an indeterminate state.
// An implementation should default to returning true for an indeterminate
// state, but may return false if there are valid reasons to do so in
// particular circumstances (e.g., performance or correctness).
//
// The param should not be held by the implementation. A copy should be
// made if the param needs to be held after the call returns.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
Enabled(ctx context.Context, param EnabledParameters) bool
}
Logger emits log records.
Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set the default behavior for unimplemented methods.
LoggerConfig
type LoggerConfig struct {
// contains filtered or unexported fields
}
LoggerConfig contains options for a Logger.
NewLoggerConfig
func NewLoggerConfig(options ...LoggerOption) LoggerConfig
NewLoggerConfig returns a new LoggerConfig with all the options applied.
LoggerConfig.InstrumentationAttributes
func (cfg LoggerConfig) InstrumentationAttributes() attribute.Set
InstrumentationAttributes returns the attributes associated with the library providing instrumentation.
LoggerConfig.InstrumentationVersion
func (cfg LoggerConfig) InstrumentationVersion() string
InstrumentationVersion returns the version of the library providing instrumentation.
LoggerConfig.SchemaURL
func (cfg LoggerConfig) SchemaURL() string
SchemaURL returns the schema URL of the library providing instrumentation.
LoggerOption
type LoggerOption interface {
// contains filtered or unexported methods
}
LoggerOption applies configuration options to a Logger.
WithInstrumentationAttributeSet
func WithInstrumentationAttributeSet(set attribute.Set) LoggerOption
WithInstrumentationAttributeSet returns a LoggerOption that adds instrumentation attributes to a Logger.
If multiple WithInstrumentationAttributes or WithInstrumentationAttributeSet options are passed, the attributes will be merged together in the order they are passed. For attributes with duplicate keys, the last value passed will be used.
WithInstrumentationAttributes
func WithInstrumentationAttributes(attr ...attribute.KeyValue) LoggerOption
WithInstrumentationAttributes returns a LoggerOption that sets the instrumentation attributes of a Logger.
This is equivalent to calling WithInstrumentationAttributeSet with an attribute.Set created from a clone of the passed attributes. WithInstrumentationAttributeSet is recommended for more control.
If multiple WithInstrumentationAttributes or WithInstrumentationAttributeSet options are passed, the attributes will be merged together in the order they are passed. For attributes with duplicate keys, the last value passed will be used.
WithInstrumentationVersion
func WithInstrumentationVersion(version string) LoggerOption
WithInstrumentationVersion returns a LoggerOption that sets the instrumentation version of a Logger.
WithSchemaURL
func WithSchemaURL(schemaURL string) LoggerOption
WithSchemaURL returns a LoggerOption that sets the schema URL for a Logger.
LoggerProvider
type LoggerProvider interface {
// Users of the interface can ignore this. This embedded type is only used
// by implementations of this interface. See the "API Implementations"
// section of the package documentation for more information.
embedded.LoggerProvider
// Logger returns a new [Logger] with the provided name and configuration.
//
// The name needs to uniquely identify the source of logged code. It is
// recommended that name be the Go package name of the library using a log
// bridge (note: this is not the name of the bridge package). Most
// commonly, this means a bridge will need to accept this value from its
// users.
//
// An empty name is invalid. Implementations should retain the empty value as the
// instrumentation scope name, return a working Logger, and report the invalid value.
//
// The version of the package using a bridge can be critical information
// to include when logging. The bridge should accept this version
// information and use the [WithInstrumentationVersion] option to configure
// the Logger appropriately.
//
// Implementations of this method need to be safe for a user to call
// concurrently.
Logger(name string, options ...LoggerOption) Logger
}
LoggerProvider provides access to Logger.
Warning: Methods may be added to this interface in minor releases. See package documentation on API implementation for information on how to set the default behavior for unimplemented methods.
Record
type Record struct {
// contains filtered or unexported fields
}
Record represents a log record. A log record with a non-empty event name is interpreted as an event record.
Record.AddAttributes
func (r *Record) AddAttributes(attrs ...attribute.KeyValue)
AddAttributes adds attributes to the log record.
Record.AttributesLen
func (r *Record) AttributesLen() int
AttributesLen returns the number of attributes in the log record.
Record.Body
func (r *Record) Body() attribute.Value
Body returns the body of the log record.
Record.Clone
func (r *Record) Clone() Record
Clone returns a copy of the record with no shared state. The original record and the clone can both be modified without interfering with each other.
Record.Err
func (r *Record) Err() error
Err returns the associated error if one has been set.
Record.EventName
func (r *Record) EventName() string
EventName returns the event name. A log record with a non-empty event name is interpreted as an event record.
Record.ObservedTimestamp
func (r *Record) ObservedTimestamp() time.Time
ObservedTimestamp returns the time when the log record was observed.
Record.SetBody
func (r *Record) SetBody(v attribute.Value)
SetBody sets the body of the log record.
Record.SetErr
func (r *Record) SetErr(err error)
SetErr sets the associated error. Passing nil clears the error.
Record.SetEventName
func (r *Record) SetEventName(s string)
SetEventName sets the event name. A log record with a non-empty event name is interpreted as an event record. Event names should uniquely identify the structure of the event's attributes and body.
Record.SetObservedTimestamp
func (r *Record) SetObservedTimestamp(t time.Time)
SetObservedTimestamp sets the time when the log record was observed.
Record.SetSeverity
func (r *Record) SetSeverity(level Severity)
SetSeverity sets the Severity level of the log record.
Record.SetSeverityText
func (r *Record) SetSeverityText(text string)
SetSeverityText sets the text of the severity (also known as the log level) as originally known at the source.
Record.SetTimestamp
func (r *Record) SetTimestamp(t time.Time)
SetTimestamp sets the time when the log record occurred.
Record.Severity
func (r *Record) Severity() Severity
Severity returns the Severity of the log record.
Record.SeverityText
func (r *Record) SeverityText() string
SeverityText returns the text of the severity (also known as the log level) as originally known at the source.
Record.Timestamp
func (r *Record) Timestamp() time.Time
Timestamp returns the time when the log record occurred.
Record.WalkAttributes
func (r *Record) WalkAttributes(f func(attribute.KeyValue) bool)
WalkAttributes walks all attributes in the log record by calling f for each attribute.KeyValue in the Record. Iteration stops if f returns false.
Severity
type Severity int
Severity represents a log record's severity (also known as its log level). Smaller numerical values correspond to less severe log records (such as debug events); larger numerical values correspond to more severe log records (such as errors and critical events).
SeverityUndefined (31 declarations)
const ( // SeverityUndefined represents an unset Severity. SeverityUndefined Severity = 0 // UNDEFINED // The following are severity values for fine-grained debugging log records. // They are typically disabled in default configurations. SeverityTrace1 Severity = 1 // TRACE SeverityTrace2 Severity = 2 // TRACE2 SeverityTrace3 Severity = 3 // TRACE3 SeverityTrace4 Severity = 4 // TRACE4 // The following are severity values for debugging log records. SeverityDebug1 Severity = 5 // DEBUG SeverityDebug2 Severity = 6 // DEBUG2 SeverityDebug3 Severity = 7 // DEBUG3 SeverityDebug4 Severity = 8 // DEBUG4 // The following are severity values for informational log records indicating // that an event happened. SeverityInfo1 Severity = 9 // INFO SeverityInfo2 Severity = 10 // INFO2 SeverityInfo3 Severity = 11 // INFO3 SeverityInfo4 Severity = 12 // INFO4 // The following are severity values for warning log records. These records // are not errors, but they are likely more important than informational // events. SeverityWarn1 Severity = 13 // WARN SeverityWarn2 Severity = 14 // WARN2 SeverityWarn3 Severity = 15 // WARN3 SeverityWarn4 Severity = 16 // WARN4 // The following are severity values for error log records indicating that // something went wrong. SeverityError1 Severity = 17 // ERROR SeverityError2 Severity = 18 // ERROR2 SeverityError3 Severity = 19 // ERROR3 SeverityError4 Severity = 20 // ERROR4 // The following are severity values for fatal log records, such as those // associated with an application or system crash. SeverityFatal1 Severity = 21 // FATAL SeverityFatal2 Severity = 22 // FATAL2 SeverityFatal3 Severity = 23 // FATAL3 SeverityFatal4 Severity = 24 // FATAL4 // The following are convenience definitions for the base severity of each // level. SeverityTrace = SeverityTrace1 SeverityDebug = SeverityDebug1 SeverityInfo = SeverityInfo1 SeverityWarn = SeverityWarn1 SeverityError = SeverityError1 SeverityFatal = SeverityFatal1 )
The following Severity values are defined by OpenTelemetry.
Severity.String
func (i Severity) String() string