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

proposal: log/slog: add iterator form of Record.Attrs #61934

Open
jba opened this issue Aug 10, 2023 · 5 comments
Open

proposal: log/slog: add iterator form of Record.Attrs #61934

jba opened this issue Aug 10, 2023 · 5 comments
Labels
Milestone

Comments

@jba
Copy link
Contributor

jba commented Aug 10, 2023

log/slog.Record.Attrs is used to iterate over the attributes stored in a record:

func (Record) Attrs(f func(Attr) bool)

If #61897 is accepted, we propose adding a method that fits better with that proposal:

func (Record) AllAttrs() iter.Seq[Attr]
@jba jba added the Proposal label Aug 10, 2023
@gopherbot gopherbot added this to the Proposal milestone Aug 10, 2023
@AndrewHarrisSPU
Copy link

Would we need func (Record) AllAttrs() iter.Seq2[[]string, Attr] to match the []string group we're in? There's also a func(Record) AllGroups() iter.Seq[[]string, iter.Seq[Attr]] possible here, hmm...

@jba
Copy link
Contributor Author

jba commented Aug 10, 2023

@AndrewHarrisSPU I don't think either of those are necessary. You may be confusing Record.Attrs with the HandleOptions.ReplaceAttr function, which does take a []string with the group names.

Record.Attrs would be used by a handler in its Handle method. If it has been paying attention to WithGroup calls, the handler knows the open groups.

@AndrewHarrisSPU
Copy link

Record.Attrs would be used by a handler in its Handle method. If it has been paying attention to WithGroup calls, the handler knows the open groups.

It knows the open groups, but it may encounter more grouping or nesting in the record attributes? I guess Record.Attrs doesn't expand groups. I may be confused about the intended behavior. Also I was unclear before whether it would be tractable to discriminate between a and b's id in log.Info(".", slog.Group("a", slog.Int("id", 0)), slog.Group("b", slog.Int("id", 0))) - it is, it's not immediate but it can be done, I'm not concerned.

@jba
Copy link
Contributor Author

jba commented Aug 11, 2023

Record.Attrs just gives you the flat, top-level sequence of Attrs. You have to notice the groups and visit their elements yourself. See the Handle and appendAttr methods in the handler-writing guide.

To answer your question, yes, the ids can be distinguished.

@willfaught
Copy link
Contributor

How about AttrsIter instead of AllAttrs? AllAttrs seems to imply that Attrs doesn't give you all the attrs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants