Skip to content

Commit

Permalink
feat: load attributes from context
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Apr 26, 2024
1 parent 105b484 commit 86ca344
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ type Option struct {

// optional: customize json payload builder
Converter Converter
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr

// optional: see slog.HandlerOptions
AddSource bool
Expand Down Expand Up @@ -131,6 +133,41 @@ func main() {
}
```

### Tracing

Import the samber/slog-otel library.

```go
import (
slogzap "github.com/samber/slog-zap"
slogotel "github.com/samber/slog-otel"
"go.opentelemetry.io/otel/sdk/trace"
)

func main() {
tp := trace.NewTracerProvider(
trace.WithSampler(trace.AlwaysSample()),
)
tracer := tp.Tracer("hello/world")

ctx, span := tracer.Start(context.Background(), "foo")
defer span.End()

span.AddEvent("bar")

logger := slog.New(
slogzap.Option{
// ...
AttrFromContext: []func(ctx context.Context) []slog.Attr{
slogotel.ExtractOtelAttrFromContext([]string{"tracing"}, "trace_id", "span_id"),
},
}.NewZapHandler(),
)

logger.ErrorContext(ctx, "a message")
}
```

## 🤝 Contributing

- Ping me on twitter [@samuelberthe](https://twitter.com/samuelberthe) (DMs, mentions, whatever :))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/samber/slog-zap/v2
go 1.21

require (
github.com/samber/slog-common v0.15.2
github.com/samber/slog-common v0.16.0
go.uber.org/goleak v1.2.1
go.uber.org/zap v1.26.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/samber/slog-common v0.15.2 h1:jQQK2MzJ1kfEsUyxI/mpwkqB2xI0qCo9Ne1D1yziuP0=
github.com/samber/slog-common v0.15.2/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/samber/slog-common v0.16.0 h1:2/t1EcFd1Ru77mh2ab+8B6NBHnEXsBBHtOJc7PSH0aI=
github.com/samber/slog-common v0.16.0/go.mod h1:Qjrfhwk79XiCIhBj8+jTq1Cr0u9rlWbjawh3dWXzaHk=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
Expand Down
9 changes: 8 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Option struct {

// optional: zap logger (default: zap.L())
Logger *zap.Logger
// optional: fetch attributes from context
AttrFromContext []func(ctx context.Context) []slog.Attr

// optional: customize json payload builder
Converter Converter
Expand All @@ -36,6 +38,10 @@ func (o Option) NewZapHandler() slog.Handler {
o.Logger = zap.L()
}

if o.AttrFromContext == nil {
o.AttrFromContext = []func(ctx context.Context) []slog.Attr{}
}

return &ZapHandler{
option: o,
attrs: []slog.Attr{},
Expand All @@ -62,7 +68,8 @@ func (h *ZapHandler) Handle(ctx context.Context, record slog.Record) error {
}

level := LogLevels[record.Level]
fields := converter(h.option.AddSource, h.option.ReplaceAttr, h.attrs, h.groups, &record)
fromContext := slogcommon.ContextExtractor(ctx, h.option.AttrFromContext)
fields := converter(h.option.AddSource, h.option.ReplaceAttr, append(h.attrs, fromContext...), h.groups, &record)

checked := h.option.Logger.Check(level, record.Message)
if checked != nil {
Expand Down

0 comments on commit 86ca344

Please sign in to comment.