Skip to content

Commit

Permalink
Quote all values that contain non-letters and non-digits.
Browse files Browse the repository at this point in the history
This makes this package work nicely with colog (comail.io/go/colog).
The colog package does not work with embedded quotes, however. Might need to send a PR.
  • Loading branch information
jjeffery committed Apr 24, 2017
1 parent f2ebab7 commit e16ed12
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions logfmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strings"
"unicode"
"unicode/utf8"
)

Expand Down Expand Up @@ -256,13 +257,10 @@ func writeTextMarshalerValue(buf *bytes.Buffer, t encoding.TextMarshaler) {
}

func needsQuote(c rune) bool {
// The single quote '\'' and equals '=' are not strictly necessary, but
// the output is more human readable if they are quoted. The colon ':'
// is quoted so that messages separated by colons are more clear.
// cannot open directory dirname="c": file not found
// is different to
// cannot open directory dirname="c:" file not found
return c <= ' ' || c == '"' || c == '\\' || c == '\'' || c == '=' || c == ':'
// This test will result in more values being quoted than is strictly
// necesary for logfmt, but quoting all non-letter and non-digits makes
// this compatible with the default colog extractor.
return !unicode.IsLetter(c) && !unicode.IsDigit(c) && c != '_'
}

func needsBackslash(c rune) bool {
Expand Down
2 changes: 1 addition & 1 deletion logfmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func TestWriteKeyValue(t *testing.T) {
{
key: struct{ v int }{v: 25},
value: struct{ v int }{v: 17},
want: "{25}={17}",
want: `{25}="{17}"`,
},
{
key: "key",
Expand Down

0 comments on commit e16ed12

Please sign in to comment.