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

Bugfix/log entry set fields #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

stefansaasen
Copy link

LogEntrySetFields: Use the varargs syntax to supply the additional log arguments correctly

Without the change in this changeset, running go test fails with:

./httplog.go:361:37: slog.Logger.With arg "attrs" should be a string or a slog.Attr (possible missing key or value)
FAIL

This is due to the fact that the array of slog.Attrs was supplied as
a single arg (i.e. a single slice of attributes) to
*entry.Logger.With, instead of a variable number of arguments.

Example:

fields := map[string]any{
        "remote": "example.com",
        "action": "update",
}
httplog.LogEntrySetFields(ctx, fields)

Without this change, the updated example that uses LogEntrySetFields
creates the following log entry:

user: "user1" !BADKEY: [remote=example.com action=update]

With the change the log line for the updated example is:

user: "user1" remote: "example.com" action: "update"

Copy link

@AnomalRoil AnomalRoil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively patching the current problem of the misuse of the variadic arguments in With.

attrs := make([]slog.Attr, len(fields))
attrs := make([]any, len(fields))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change really necessary?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*entry.Logger.With(attrs...) has the following signature:

func (l *Logger) With(args ...any)

image

…g arguments correctly

Without the change in this changeset, running `go test` fails with:

```
./httplog.go:361:37: slog.Logger.With arg "attrs" should be a string or a slog.Attr (possible missing key or value)
FAIL
```

This is due to the fact that the array of `slog.Attr`s was supplied as
a single arg (i.e. a single slice of attributes) to
`*entry.Logger.With`, instead of a variable number of arguments.

Example:

```go
fields := map[string]any{
        "remote": "example.com",
        "action": "update",
}
httplog.LogEntrySetFields(ctx, fields)
```

Without this change, the updated example that uses LogEntrySetFields
creates the following log entry:

```
user: "user1" !BADKEY: [remote=example.com action=update]
```

With the change the log line for the updated example is:

```
user: "user1" remote: "example.com" action: "update"
```
@stefansaasen
Copy link
Author

Updated the PR to incorporate the latest changes from master.

Copy link

@AnomalRoil AnomalRoil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'm not the maintainer, just a user ^^'

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

Successfully merging this pull request may close these issues.

None yet

2 participants