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

Add support for hex-encoded bytes #42

Merged
merged 3 commits into from
Mar 15, 2018
Merged

Add support for hex-encoded bytes #42

merged 3 commits into from
Mar 15, 2018

Conversation

awfm9
Copy link
Contributor

@awfm9 awfm9 commented Mar 15, 2018

This PR adds support for hex-encoded bytes by using the Hex(...) functions instead of the Bytes(...) 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(...) than Bytes(...) should not matter, as it is optional to use and it would incure the same overhead when manually converting each time.

@awfm9
Copy link
Contributor Author

awfm9 commented Mar 15, 2018

I also fixed up some tests that were missing the Bytes(...) function call.

event.go Outdated
if e == nil {
return e
}
e.buf = json.AppendString(json.AppendKey(e.buf, key), hex.EncodeToString(val))
Copy link
Owner

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.

Copy link
Contributor Author

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.

for _, v := range s {
dst = append(dst, hex[v>>4], hex[v&0x0f])
}
dst = append(dst, s...)
Copy link
Owner

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.

Copy link
Contributor Author

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.

@awfm9
Copy link
Contributor Author

awfm9 commented Mar 15, 2018

It should be OK now.

@rs
Copy link
Owner

rs commented Mar 15, 2018

Thx for your contrib 👍

@awfm9
Copy link
Contributor Author

awfm9 commented Mar 15, 2018

Thank you for the nice logging library 👍

@rs rs merged commit 1c575db into rs:master Mar 15, 2018
@rs rs mentioned this pull request Apr 27, 2018
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