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 WithAttrs and Logger.WithAttrs #66937

Open
mohammadv184 opened this issue Apr 20, 2024 · 5 comments
Open

proposal: log/slog: Add WithAttrs and Logger.WithAttrs #66937

mohammadv184 opened this issue Apr 20, 2024 · 5 comments
Labels
Milestone

Comments

@mohammadv184
Copy link
Contributor

mohammadv184 commented Apr 20, 2024

Proposal Details

This proposal addresses a deficiency in the log/slog package, specifically the lack of support for purely attribute-based logging functions/methods. The primary focus of this proposal is the introduction of a new function, WithAttrs, to both the package scope and the Logger interface. This addition seeks to streamline and enhance logging practices, particularly in scenarios such as:

attrs := []slog.Attr{
    {
        Key:   "foo",
        Value: "bar",
    },
    {
        Key:   "bar",
        Value: "foo",
    },
}

slog.WithAttrs(attrs...)
@gopherbot gopherbot added this to the Proposal milestone Apr 20, 2024
mohammadv184 added a commit to mohammadv184/go that referenced this issue Apr 20, 2024
This merge request introduces the WithAttrs function to the log/slog package, enabling efficient attribute-based logging of structured data.

Updates golang#66937
@gopherbot
Copy link

Change https://go.dev/cl/580495 mentions this issue: log/slog: Add WithAttrs and Logger.WithAttrs

@ianlancetaylor
Copy link
Contributor

CC @jba

@jba
Copy link
Contributor

jba commented Apr 22, 2024

It's unfortunate that you can't write logger = logger.With(attrs...). But you can get the same effect with

for _, a := range attrs {
    logger = logger.With(a)
}

That is less efficient, but the question is whether it comes up often enough to matter.

@mohammadv184
Copy link
Contributor Author

It's unfortunate that you can't write logger = logger.With(attrs...). But you can get the same effect with

for _, a := range attrs {
    logger = logger.With(a)
}

That is less efficient, but the question is whether it comes up often enough to matter.

While the suggested workaround using a loop to iterate over the attributes and apply them individually with logger.With(a) is indeed functional, as you noted, it may introduce some inefficiency, particularly in scenarios where a large number of attributes need to be logged.

Considering your point about whether the inefficiency of the loop comes up often enough to matter, I think it's important. However, introducing a dedicated function like WithAttrs would provide a more streamlined and efficient solution for handling attribute-based logging, ultimately improving code readability and maintainability.

@mohammadv184
Copy link
Contributor Author

Just as LogAttrs offers a specialized solution for logging structured data with attributes, a dedicated WithAttrs function would serve a similar purpose for attribute-based logging within the context of With.

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