Skip to content

Commit

Permalink
Fixed wrong display of the source in presence of a ReplaceAttr (#39)
Browse files Browse the repository at this point in the history
* added test case

* fixing source rendering

---------

Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
  • Loading branch information
lmittmann and lmittmann committed Sep 13, 2023
1 parent 8c0ec83 commit f895639
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
23 changes: 13 additions & 10 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {
if a.Value.Kind() == slog.KindTime {
h.appendTime(buf, a.Value.Time())
} else {
appendValue(buf, a.Value, false)
h.appendValue(buf, a.Value, false)
}
buf.WriteByte(' ')
}
Expand All @@ -195,7 +195,7 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {
if a.Value.Kind() == slog.KindInt64 {
h.appendLevel(buf, slog.Level(a.Value.Int64()))
} else {
appendValue(buf, a.Value, false)
h.appendValue(buf, a.Value, false)
}
buf.WriteByte(' ')
}
Expand All @@ -215,7 +215,7 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {
h.appendSource(buf, src)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.Any(slog.SourceKey, src)); a.Key != "" {
appendValue(buf, a.Value, false)
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}
}
Expand All @@ -226,7 +226,7 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error {
buf.WriteString(r.Message)
buf.WriteByte(' ')
} else if a := rep(nil /* groups */, slog.String(slog.MessageKey, r.Message)); a.Key != "" {
appendValue(buf, a.Value, false)
h.appendValue(buf, a.Value, false)
buf.WriteByte(' ')
}

Expand Down Expand Up @@ -357,7 +357,7 @@ func (h *handler) appendAttr(buf *buffer, attr slog.Attr, groupsPrefix string) {
fallthrough
default:
h.appendKey(buf, attr.Key, groupsPrefix)
appendValue(buf, attr.Value, true)
h.appendValue(buf, attr.Value, true)
buf.WriteByte(' ')
}
}
Expand All @@ -369,7 +369,7 @@ func (h *handler) appendKey(buf *buffer, key, groups string) {
buf.WriteStringIf(!h.noColor, ansiReset)
}

func appendValue(buf *buffer, v slog.Value, quote bool) {
func (h *handler) appendValue(buf *buffer, v slog.Value, quote bool) {
switch v.Kind() {
case slog.KindString:
appendString(buf, v.String(), quote)
Expand All @@ -386,15 +386,18 @@ func appendValue(buf *buffer, v slog.Value, quote bool) {
case slog.KindTime:
appendString(buf, v.Time().String(), quote)
case slog.KindAny:
if tm, ok := v.Any().(encoding.TextMarshaler); ok {
data, err := tm.MarshalText()
switch cv := v.Any().(type) {
case encoding.TextMarshaler:
data, err := cv.MarshalText()
if err != nil {
break
}
appendString(buf, string(data), quote)
break
case *slog.Source:
h.appendSource(buf, cv)
default:
appendString(buf, fmt.Sprint(v.Any()), quote)
}
appendString(buf, fmt.Sprint(v.Any()), quote)
}
}

Expand Down
12 changes: 12 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ func TestHandler(t *testing.T) {
},
Want: `group.key=val`,
},
{ // https://github.com/lmittmann/tint/pull/37
Opts: &tint.Options{
AddSource: true,
ReplaceAttr: func(g []string, a slog.Attr) slog.Attr {
return a
},
},
F: func(l *slog.Logger) {
l.Info("test")
},
Want: `Nov 10 23:00:00.000 INF tint/handler_test.go:303 test`,
},
}

for i, test := range tests {
Expand Down

0 comments on commit f895639

Please sign in to comment.