Slog
Slog is a structured logging library for Go, introduced in Go 1.21. This guide demonstrates how to integrate slog with Sentry to capture and send logs to Sentry.
For a quick reference, there is a complete example at the Go SDK source code repository.
Go API documentation for the sentryslog
package is also available.
Slog structured logging is supported in Sentry Go SDK version 0.34.0
and above.
Install the sentryslog
package:
go get github.com/getsentry/sentry-go/slog
To enable logging, you need to initialize the SDK with the EnableLogs
option set to true.
sentry.Init(sentry.ClientOptions{
Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Enable logs to be sent to Sentry
EnableLogs: true,
})
sentryslog
provides options to configure the integration with Sentry. It accepts a struct of sentryslog.Options
that allows you to configure how the handler will behave. The options are:
type Option struct {
// EventLevel specifies the exact log levels to capture and send to Sentry as Events.
// Only logs at these specific levels will be processed as events.
// Defaults to []slog.Level{slog.LevelError, LevelFatal}.
EventLevel []slog.Level
// LogLevel specifies the exact log levels to capture and send to Sentry as Log entries.
// Only logs at these specific levels will be processed as log entries.
// Defaults to []slog.Level{slog.LevelDebug, slog.LevelInfo, slog.LevelWarn, slog.LevelError, LevelFatal}.
LogLevel []slog.Level
// Hub specifies the Sentry Hub to use for capturing events.
// If not provided, the current Hub is used by default.
Hub *sentry.Hub
// Converter is an optional function that customizes how log records
// are converted into Sentry events. By default, the DefaultConverter is used.
Converter Converter
// AttrFromContext is an optional slice of functions that extract attributes
// from the context. These functions can add additional metadata to the log entry.
AttrFromContext []func(ctx context.Context) []slog.Attr
// AddSource is an optional flag that, when set to true, includes the source
// information (such as file and line number) in the Sentry event.
// This can be useful for debugging purposes.
AddSource bool
// ReplaceAttr is an optional function that allows for the modification or
// replacement of attributes in the log record. This can be used to filter
// or transform attributes before they are sent to Sentry.
ReplaceAttr func(groups []string, a slog.Attr) slog.Attr
}
To integrate Sentry with slog, you need to set up a Sentry handler and configure the Sentry client.
func main() {
if err := sentry.Init(sentry.ClientOptions{
Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
EnableLogs: true,
}); err != nil {
log.Fatalf("Sentry initialization failed: %v", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
defer sentry.Flush(2 * time.Second)
ctx := context.Background()
// Configure the sentryslog handler.
// The handler lets you explicitly declare how to handle each log level.
// You can handle logs as events or as log entries by specifying the `EventLevel` and `LogLevel` slices.
handler := sentryslog.Option{
EventLevel: []slog.Level{slog.LevelError}, // Only LevelError entries will be sent as events
LogLevel: []slog.Level{slog.LevelWarn, slog.LevelInfo}, // Only LevelWarn and LevelInfo entries will be sent as logs
}.NewSentryHandler(ctx)
logger := slog.New(handler)
logger = logger.With("release", "v1.0.0")
// Log messages with various attributes
logger.
With(
slog.Group("user",
slog.String("id", "user-123"),
slog.Time("created_at", time.Now()),
),
).
With("environment", "dev").
With("error", fmt.Errorf("an error")).
ErrorContext(ctx, "a message")
}
In order to properly attach the correct trace with each log entry, a context.Context
is required. If you're using logs combined with tracing, you should pass the correct context to properly attach each trace with the appropriate logs.
For comprehensive logging setup with slog, including advanced configuration options and best practices, see the Go Logs documentation. The slog integration shown above provides seamless integration with Sentry's structured logging features.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").