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 ¶
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 ¶
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.