Documentation
¶
Overview ¶
Package otelzerolog provides a Hook, a zerolog.Hook implementation that can be used to bridge between the zerolog API and OpenTelemetry.
Record Conversion ¶
The zerolog.Event records are converted to OpenTelemetry log.Record in the following way:
- Message is set as the Body using a log.StringValue.
- Level is transformed and set as the Severity. The SeverityText is also set.
The Level is transformed to the OpenTelemetry Severity types in the following way.
- zerolog.DebugLevel is transformed to log.SeverityDebug
- zerolog.InfoLevel is transformed to log.SeverityInfo
- zerolog.WarnLevel is transformed to log.SeverityWarn
- zerolog.ErrorLevel is transformed to log.SeverityError
- zerolog.PanicLevel is transformed to log.SeverityFatal1
- zerolog.FatalLevel is transformed to log.SeverityFatal2
NOTE: Fields are not transformed because of https://github.com/rs/zerolog/issues/493.
Example ¶
package main
import (
"os"
"github.com/rs/zerolog"
"go.opentelemetry.io/contrib/bridges/otelzerolog"
"go.opentelemetry.io/otel/log/noop"
)
func main() {
// Use a working LoggerProvider implementation instead e.g. using go.opentelemetry.io/otel/sdk/log.
provider := noop.NewLoggerProvider()
// Create a logger that emits logs to both STDOUT and the OTel Go SDK.
hook := otelzerolog.NewHook("my/pkg/name", otelzerolog.WithLoggerProvider(provider))
logger := zerolog.New(os.Stdout).With().Logger()
logger = logger.Hook(hook)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hook ¶
type Hook struct {
// contains filtered or unexported fields
}
Hook is a zerolog.Hook that sends all logging records it receives to OpenTelemetry. See package documentation for how conversions are made.
func NewHook ¶
NewHook returns a new Hook to be used as a [Zerolog.Hook]. If WithLoggerProvider is not provided, the returned Hook will use the global LoggerProvider.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures a Hook.
func WithLoggerProvider ¶
func WithLoggerProvider(provider log.LoggerProvider) Option
WithLoggerProvider returns an Option that configures log.LoggerProvider used by a Hook.
By default if this Option is not provided, the Hook will use the global LoggerProvider.
func WithSchemaURL ¶
WithSchemaURL returns an Option that configures the semantic convention schema URL of the log.Logger used by a Hook. The schemaURL should be the schema URL for the semantic conventions used in log records.
func WithVersion ¶
WithVersion returns an Option that configures the version of the log.Logger used by a Hook. The version should be the version of the package that is being logged.