Skip to content

Commit

Permalink
Bump slog (#19)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
karlmutch and lmittmann authored Apr 21, 2023
1 parent 5f32449 commit 3ce584b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
9 changes: 7 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
25 changes: 21 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down Expand Up @@ -216,15 +216,32 @@ 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) {
l.Info("test", "key", "val")
},
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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ce584b

Please sign in to comment.