Skip to content

Slog integration only attaches to trace when using slog.***Context() #1117

@Gitznik

Description

@Gitznik

How do you use Sentry?

Sentry SaaS (sentry.io)

SDK version

0.35.3

Go version

1.25.2

Are you using Go Modules?

yes

Steps to reproduce

package logger

import (
	"context"
	"log/slog"

	sentryslog "github.com/getsentry/sentry-go/slog"
)

func NewSentryLogger(ctx context.Context) *Logger {
	handler := sentryslog.Option{
		EventLevel: []slog.Level{slog.LevelError},
		LogLevel:   []slog.Level{slog.LevelWarn, slog.LevelInfo},
		AddSource:  true,
	}.NewSentryHandler(ctx)
    slogger := slog.New(handler)
    slogger.Info("I should attach to the trace in `ctx`")

Expected result

I expected from the docs that the info log would be attached to the trace in the context I provided to NewSentryHandler.

Actual result

slogger.Info creates a new context.Background(), so my log is not attached to the original trace. To do so, I would have to call slogger.InfoContext(ctx, ...) on every log. If this is intended behavior, I found the docs extremely confusing.

Metadata

Metadata

Assignees

Labels

No fields configured for issues without a type.

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions