-
Notifications
You must be signed in to change notification settings - Fork 573
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
Resolve panic in the console writer for short level
field values
#665
Conversation
zerolog
for Short level
Field Valueszerolog
for short level
field values
console.go
Outdated
@@ -401,21 +401,38 @@ func consoleDefaultFormatTimestamp(timeFormat string, location *time.Location, n | |||
} | |||
|
|||
func consoleDefaultFormatLevel(noColor bool) Formatter { | |||
prefix := func(s string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this a top level function?
console.go
Outdated
if len(ll) == 0 { | ||
l = unknownLevel | ||
} else { | ||
l = strings.ToUpper(prefix(ll)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about making all this logic a function instead of just prefix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
zerolog
for short level
field valueslevel
field values
console.go
Outdated
@@ -401,24 +401,29 @@ func consoleDefaultFormatTimestamp(timeFormat string, location *time.Location, n | |||
} | |||
|
|||
func consoleDefaultFormatLevel(noColor bool) Formatter { | |||
const unknownLevel = "???" | |||
stripLevel := func(ll string) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason for this function not to be top level?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, I try to reduce the scope of visibility as much as possible (especially when the stuff is too specific and doesn't look like it's to be reused elsewhere), but I don't have any strong preferences. So, if you think it's better to extract it from the function to the package I'll do it promptly. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer smaller unit functions as they enhance readability and do not eliminate the possibility of inlining.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Done. :)
This PR fixes an issue in the
zerolog
console writer. Specifying a non-typical value for thelevel
field, resulting in a serialized value of less than 3 bytes, causes a panic. For instance: