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

v2: add Hex helpers and dont quote values with newlines #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions v2/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,29 @@ func Hex(key string, value []byte) slog.Attr {
// Hex6 is a convenience function for hex-encoded log attributes which prints
// a maximum of 6 bytes.
func Hex6(key string, value []byte) slog.Attr {
if len(value) <= 6 {
return HexN(key, value, 6)
}

// Hex3 is a convenience function for hex-encoded log attributes which prints
// a maximum of 3 bytes.
func Hex3(key string, value []byte) slog.Attr {
return HexN(key, value, 3)
}

// Hex2 is a convenience function for hex-encoded log attributes which prints
// a maximum of 2 bytes.
func Hex2(key string, value []byte) slog.Attr {
return HexN(key, value, 2)
}

// HexN is a convenience function for hex-encoded log attributes which prints
// a maximum of n bytes.
func HexN(key string, value []byte, n uint) slog.Attr {
if len(value) <= int(n) {
return slog.String(key, hex.EncodeToString(value))
}

return slog.String(key, hex.EncodeToString(value[:6]))
return slog.String(key, hex.EncodeToString(value[:n]))
}

// Fmt returns a slog.Attr with the formatted message which is only computed
Expand Down
3 changes: 3 additions & 0 deletions v2/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ var tests = []struct {
log.InfoS(ctx, "Multi word string value", "key with spaces", "value")
log.InfoS(ctx, "Number attribute", "key", 5)
log.InfoS(ctx, "Bad key", "key")
log.InfoS(ctx, "Log with new line", "key", "value\nvalue")

type b struct {
name string
Expand All @@ -160,6 +161,8 @@ var tests = []struct {
[INF]: Multi word string value "key with spaces"=value
[INF]: Number attribute key=5
[INF]: Bad key !BADKEY=key
[INF]: Log with new line key=value
value
[INF]: Nil pointer value key=<nil>
[INF]: Struct values key="&{name:Bob age:5 address:<nil>}"
[INF]: Test context attributes request_id=5 user_name=alice key=value
Expand Down
3 changes: 3 additions & 0 deletions v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func needsQuoting(s string) bool {
if len(s) == 0 {
return true
}
if strings.ContainsRune(s, '\n') {
return false
}
for i := 0; i < len(s); {
b := s[i]
if b < utf8.RuneSelf {
Expand Down