-
Notifications
You must be signed in to change notification settings - Fork 578
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
Add support for hex-encoded bytes #42
Conversation
I also fixed up some tests that were missing the |
event.go
Outdated
if e == nil { | ||
return e | ||
} | ||
e.buf = json.AppendString(json.AppendKey(e.buf, key), hex.EncodeToString(val)) |
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.
You could create a json.AppendHex
that just does:
func AppendHex(buf, val []byte) []byte {
const hextable = "0123456789abcdef"
for _, v := range val {
buf = append(buf, hextable[v>>4], hextable[v&0x0f])
}
return buf
}
This would avoid the slice heap allocation done by hex.EncodeToString
.
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. I also added comments for all the AppendXXX(...)
functions that were missing them to shut up the linter.
internal/json/string.go
Outdated
for _, v := range s { | ||
dst = append(dst, hex[v>>4], hex[v&0x0f]) | ||
} | ||
dst = append(dst, s...) |
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 don't understand this line.
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.
Stupid copy-paste mistake. Fixing it up with the tests now.
It should be OK now. |
Thx for your contrib 👍 |
Thank you for the nice logging library 👍 |
This PR adds support for hex-encoded bytes by using the
Hex(...)
functions instead of theBytes(...)
functions. Most of the time, when logging a byte slice, this is what we want.I did not find a more efficient way to do this. It's a lot of overhead to manually encode each byte slice to a hex string whenever we want to log it in such a way, so I think the PR has merrit, even if it's a bit of a convenience feature.
The fact it's less efficient to use
Hex(...)
thanBytes(...)
should not matter, as it is optional to use and it would incure the same overhead when manually converting each time.