Skip to content

Commit

Permalink
fixed invocation of Options.ReplaceAttr
Browse files Browse the repository at this point in the history
  • Loading branch information
lmittmann committed Oct 29, 2023
1 parent da92dd8 commit 59d9c24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 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
7 changes: 3 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,16 @@ func TestReplaceAttr(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
slogRecord := make([]replaceAttrParams, 0)
tintRecord := 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...)
slogLogger.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)
Expand Down

0 comments on commit 59d9c24

Please sign in to comment.