From 3ce584b3a1aea81fc289340724afa87d29c724b2 Mon Sep 17 00:00:00 2001 From: Karl Mutch Date: Thu, 20 Apr 2023 23:53:02 -0700 Subject: [PATCH] Bump slog (#19) * Return a boolean from the attr processing function inline with the new slog API changes * dropped old dependency * updated example to reflect new attribute-drop logic * updated drop-attribute-logic --------- Co-authored-by: lmittmann --- README.md | 2 +- go.mod | 2 +- go.sum | 4 ++-- handler.go | 9 +++++++-- handler_test.go | 25 +++++++++++++++++++++---- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c048ff8..07fcec5 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ for details. logger := slog.New(tint.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == slog.TimeKey && len(groups) == 0 { - a.Key = "" + return slog.Attr{} } return a }, diff --git a/go.mod b/go.mod index 83fbd67..e4f7b9d 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/lmittmann/tint go 1.20 -require golang.org/x/exp v0.0.0-20230321023759-10a507213a29 +require golang.org/x/exp v0.0.0-20230420155640-133eef4313cb diff --git a/go.sum b/go.sum index 4555300..6b0eb31 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= -golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +golang.org/x/exp v0.0.0-20230420155640-133eef4313cb h1:rhjz/8Mbfa8xROFiH+MQphmAmgqRM0bOMnytznhWEXk= +golang.org/x/exp v0.0.0-20230420155640-133eef4313cb/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= diff --git a/handler.go b/handler.go index 60a1030..de05d9c 100644 --- a/handler.go +++ b/handler.go @@ -190,11 +190,12 @@ func (h *handler) Handle(_ context.Context, r slog.Record) error { } // write attributes - r.Attrs(func(attr slog.Attr) { + r.Attrs(func(attr slog.Attr) bool { if rep != nil { attr = rep(h.groupsSlice, attr) } h.appendAttr(buf, attr, "") + return true }) if len(*buf) == 0 { @@ -287,9 +288,10 @@ func (h *handler) appendSource(buf *buffer, f runtime.Frame) { } func (h *handler) appendAttr(buf *buffer, attr slog.Attr, groups string) { - if attr.Key == "" { + if attr.Equal(slog.Attr{}) { return } + attr.Value = attr.Value.Resolve() switch attr.Value.Kind() { case slog.KindGroup: @@ -366,6 +368,9 @@ func appendString(buf *buffer, s string, quote bool) { } func needsQuoting(s string) bool { + if len(s) == 0 { + return true + } for _, r := range s { if unicode.IsSpace(r) || r == '"' || r == '=' || !unicode.IsPrint(r) { return true diff --git a/handler_test.go b/handler_test.go index cb4fedc..329c463 100644 --- a/handler_test.go +++ b/handler_test.go @@ -167,7 +167,7 @@ func TestHandler(t *testing.T) { Opts: tint.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if a.Key == "key" && len(groups) == 1 && groups[0] == "group" { - a.Key = "" + return slog.Attr{} } return a }, @@ -216,8 +216,7 @@ func TestHandler(t *testing.T) { { Opts: tint.Options{ ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { - a.Key = "" - return a + return slog.Attr{} }, }, F: func(l *slog.Logger) { @@ -225,6 +224,24 @@ func TestHandler(t *testing.T) { }, Want: ``, }, + { + F: func(l *slog.Logger) { + l.Info("test", "key", "") + }, + Want: `Nov 10 23:00:00.000 INF test key=""`, + }, + { + F: func(l *slog.Logger) { + l.Info("test", "", "val") + }, + Want: `Nov 10 23:00:00.000 INF test ""=val`, + }, + { + F: func(l *slog.Logger) { + l.Info("test", "", "") + }, + Want: `Nov 10 23:00:00.000 INF test ""=""`, + }, { // https://github.com/lmittmann/tint/issues/8 F: func(l *slog.Logger) { @@ -270,7 +287,7 @@ func drop(keys ...string) func([]string, slog.Attr) slog.Attr { for _, key := range keys { if a.Key == key { - a.Key = "" + a = slog.Attr{} } } return a