Skip to content

Commit

Permalink
Fixed invocation of Options.ReplaceAttr (#47)
Browse files Browse the repository at this point in the history
* added `ReplaceAttr` test

* fixed invocation of `Options.ReplaceAttr`

---------

Co-authored-by: lmittmann <[email protected]>
  • Loading branch information
lmittmann and lmittmann authored Oct 29, 2023
1 parent 0c0d0cf commit 2027ee7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
39 changes: 17 additions & 22 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,7 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {

// write attributes
r.Attrs(func(attr slog.Attr) bool {
if rep != nil {
attr = rep(h.groups, attr)
}
h.appendAttr(buf, attr, h.groupPrefix)
h.appendAttr(buf, attr, h.groupPrefix, h.groups)
return true
})

Expand All @@ -263,10 +260,7 @@ func (h *handler) WithAttrs(attrs []slog.Attr) slog.Handler {

// write attributes to buffer
for _, attr := range attrs {
if h.replaceAttr != nil {
attr = h.replaceAttr(h.groups, attr)
}
h.appendAttr(buf, attr, h.groupPrefix)
h.appendAttr(buf, attr, h.groupPrefix, h.groups)
}
h2.attrsPrefix = h.attrsPrefix + string(*buf)
return h2
Expand Down Expand Up @@ -330,29 +324,30 @@ func (h *handler) appendSource(buf *buffer, src *slog.Source) {
buf.WriteStringIf(!h.noColor, ansiReset)
}

func (h *handler) appendAttr(buf *buffer, attr slog.Attr, groupsPrefix string) {
func (h *handler) appendAttr(buf *buffer, attr slog.Attr, groupsPrefix string, groups []string) {
attr.Value = attr.Value.Resolve()
if rep := h.replaceAttr; rep != nil && attr.Value.Kind() != slog.KindGroup {
attr = rep(groups, attr)
attr.Value = attr.Value.Resolve()
}

if attr.Equal(slog.Attr{}) {
return
}
attr.Value = attr.Value.Resolve()

switch attr.Value.Kind() {
case slog.KindGroup:
if attr.Value.Kind() == slog.KindGroup {
if attr.Key != "" {
groupsPrefix += attr.Key + "."
groups = append(groups, attr.Key)
}
for _, groupAttr := range attr.Value.Group() {
h.appendAttr(buf, groupAttr, groupsPrefix)
h.appendAttr(buf, groupAttr, groupsPrefix, groups)
}
case slog.KindAny:
if err, ok := attr.Value.Any().(tintError); ok {
// append tintError
h.appendTintError(buf, err, groupsPrefix)
buf.WriteByte(' ')
break
}
fallthrough
default:
} else if err, ok := attr.Value.Any().(tintError); ok {
// append tintError
h.appendTintError(buf, err, groupsPrefix)
buf.WriteByte(' ')
} else {
h.appendKey(buf, attr.Key, groupsPrefix)
h.appendValue(buf, attr.Value, true)
buf.WriteByte(' ')
Expand Down
48 changes: 46 additions & 2 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"log/slog"
"os"
"slices"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -98,7 +99,7 @@ func TestHandler(t *testing.T) {
F: func(l *slog.Logger) {
l.Info("test", "key", "val")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:99 test key=val`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:100 test key=val`,
},
{
Opts: &tint.Options{
Expand Down Expand Up @@ -325,7 +326,7 @@ func TestHandler(t *testing.T) {
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:326 test`,
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:327 test`,
},
{ // https://github.com/lmittmann/tint/issues/44
F: func(l *slog.Logger) {
Expand Down Expand Up @@ -385,6 +386,49 @@ func replace(new slog.Value, keys ...string) func([]string, slog.Attr) slog.Attr
}
}

func TestReplaceAttr(t *testing.T) {
tests := [][]any{
{},
{"key", "val"},
{"key", "val", slog.Group("group", "key2", "val2")},
{"key", "val", slog.Group("group", "key2", "val2", slog.Group("group2", "key3", "val3"))},
}

type replaceAttrParams struct {
Groups []string
Attr slog.Attr
}

replaceAttrRecorder := func(record *[]replaceAttrParams) func([]string, slog.Attr) slog.Attr {
return func(groups []string, a slog.Attr) slog.Attr {
*record = append(*record, replaceAttrParams{groups, a})
return a
}
}

for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
slogRecord := make([]replaceAttrParams, 0)
slogLogger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{
ReplaceAttr: replaceAttrRecorder(&slogRecord),
}))
slogLogger.Log(context.TODO(), slog.LevelInfo, "", test...)

tintRecord := make([]replaceAttrParams, 0)
tintLogger := slog.New(tint.NewHandler(io.Discard, &tint.Options{
ReplaceAttr: replaceAttrRecorder(&tintRecord),
}))
tintLogger.Log(context.TODO(), slog.LevelInfo, "", test...)

if !slices.EqualFunc(slogRecord, tintRecord, func(a, b replaceAttrParams) bool {
return slices.Equal(a.Groups, b.Groups) && a.Attr.Equal(b.Attr)
}) {
t.Fatalf("(-want +got)\n- %v\n+ %v", slogRecord, tintRecord)
}
})
}
}

// See https://github.com/golang/exp/blob/master/slog/benchmarks/benchmarks_test.go#L25
//
// Run e.g.:
Expand Down

0 comments on commit 2027ee7

Please sign in to comment.