slog

package
v0.0.0-...-01f3aef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package slog provides a slog.Handler that will log to an OTEL span if one is active.

This allows logging events you record with the log or slog package to be linked into the OTEL trace if one is active. If you do not use this package, OTEL events from this package will still be recorded to the active span, however any log data you record will not be linked to the OTEL trace.

You can use this package as a wrapper around the logger you use in your application.

The best practice for using this package is to do the setup at package main.

Example usage with custom logger (package main):

func main() {
	customLogger := yourLogPackage.New()
	slog.SetDefault(events.NewOTEL(customLogger))
}

The above will always use your custom logger whenever logging is done. If there is an active OTEL span, it will log to that span too along with our trace messages.

Example usage with default logger (package main):

func main() {
	slog.SetDefault(events.NewOTEL(nil))
}

The above will always use the default logger whenever logging is done. If there is an active OTEL span, it will log to that span too along with our trace messages.

Index

Constants

View Source
const LevelTrace = slog.Level(-8)

LevelTrace is the level at which we record events to the span. This correlates with the slog documentation recommendation.

Variables

This section is empty.

Functions

func NewOTEL

func NewOTEL(h slog.Handler) slog.Handler

NewOTEL provides a new slog.Handler that can log to an active span. If there is no active span, it logs to the logger handler provided. If h == nil, it will use slog.Default() as the logger handler. If that handler is a of the OTEL type, it will use that Handler's wrapped handler. The underlying handler is always logged to.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL