Skip to content

Consider a "group" construct to match slog #199

Open
@thockin

Description

#196 (comment)

Consider:

s := logr.ToSlog(myLogger)
s.With("a", 1)  // calls Handler.WithAttrs(), which calls logr WithValues("a", 1)
s = s.WithGroup("g") // calls Handler.WithGroup("g"), which saves "g" as a prefix
s.With("b", 2)  // calls Handler.WithAttrs(), which calls logr WithValues("g.b", 2)
s.Info("the msg", "c", 3) // calls Handler.Handle, which calls logr.Info("the msg", "g.c", 3)

Unless the LogSink is very clever (parse all keys and re-group, which I would not suggest doing), the result will be something like {"msg": "the msg", "a": 1, "g.b": 2, "g.c": 3}, when what the user clearly wanted was more like {"msg": "the msg", "a": 1, "g": {"b": 2, "c": 3}}

If we teach logr to have a "group" construct, maybe we can make it work:

s := logr.ToSlog(myLogger)
s.With("a", 1)  // calls Handler.WithAttrs(), which calls logr WithValues("a", 1)
s = s.WithGroup("g") // calls Handler.WithGroup("g"), which calls logr WithGroup("g")
s.With("b", 2)  // calls Handler.WithAttrs(), which calls logr WithValues("b", 2)
s.Info("the msg", "c", 3) // calls Handler.Handle, which calls logr.Info("the msg", "c", 3)

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions