-
-
Notifications
You must be signed in to change notification settings - Fork 203
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
Bug: BasicLogger Panics When len(keyvals)
is 1
#856
Comments
See comment on PR -- I'm okay keeping a change if a "missing" field is added, but do not want to strip an extraneous extra argument. As is, the basic logger contract specifies that it should be key/value fields, and Go is no stranger to panicking on invalid usage of APIs. |
Yes, sure! I just think that from a user point of view, that wasn't very clear and, since it is suposed to be used as key/value arguments, it should also panic on an even number of elements, right? I think that if you want to ensure that behaviour, a good idea would be to follow the slog or zap loggers patterns, in wich you specify the fields such as below: logger.Log(kgo.LogLevelDebug, "main message", log.String("key", "Value"), log.Bool("key2", true)) |
Adding slog or zap APIs would expand the API surface a bit more than seemingly needed at the moment. That said, I misread your original PR -- I thought it was trimming an argument, not a trailing comma / space. My mistake, I'll merge it into the next release. |
Context
When calling
kgo.BasicLogger .Log()
with only one extra argument (len(keyvals) = 1
), it panics becauselen(keyvals) / 2
will be 0 for int variables, the second parameter ofstrings.Repeat
. Since the result is 0, the length of the format variable will be 0, which will cause an out of slice bound access. Therefore that single value should be an EXTRA attribute such as when passing an even number ofkeyvals
.Refer to PR #857
Error Reproduction
Output:
But, when passing an even number of
keyvals
greater than 1, it is treated as an EXTRA attribute and we have the following behavior:Output:
Expected Behavior
If
keyvals
length is equal to 1, it should be treated as an EXTRA as follows:The text was updated successfully, but these errors were encountered: