Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider a "group" construct to match slog #199

Open
thockin opened this issue Aug 4, 2023 · 0 comments
Open

Consider a "group" construct to match slog #199

thockin opened this issue Aug 4, 2023 · 0 comments
Assignees

Comments

@thockin
Copy link
Contributor

thockin commented Aug 4, 2023

#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)
@thockin thockin mentioned this issue Aug 4, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants