Skip to content

Commit

Permalink
quote strings in slices to reduce reading confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Feb 16, 2021
1 parent 7702d38 commit c102b1c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion interceptlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestInterceptLogger(t *testing.T) {
output := buf.String()
dataIdx := strings.IndexByte(output, ' ')
rest := output[dataIdx+1:]
assert.Equal(t, "[DEBUG] with_test.sub_logger.http: test1: parent=logger path=/some/test/path args=[test, test]\n", rest)
assert.Equal(t, "[DEBUG] with_test.sub_logger.http: test1: parent=logger path=/some/test/path args=[\"test\", \"test\"]\n", rest)
})

t.Run("derived standard loggers send output to sinks", func(t *testing.T) {
Expand Down
13 changes: 5 additions & 8 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,22 +379,19 @@ func (l *intLogger) renderSlice(v reflect.Value) string {

switch sv.Kind() {
case reflect.String:
val = sv.String()
val = strconv.Quote(sv.String())
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
val = strconv.FormatInt(sv.Int(), 10)
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val = strconv.FormatUint(sv.Uint(), 10)
default:
val = fmt.Sprintf("%v", sv.Interface())
if strings.ContainsAny(val, " \t\n\r") {
val = strconv.Quote(val)
}
}

if strings.ContainsAny(val, " \t\n\r") {
buf.WriteByte('"')
buf.WriteString(val)
buf.WriteByte('"')
} else {
buf.WriteString(val)
}
buf.WriteString(val)
}

buf.WriteRune(']')
Expand Down
4 changes: 2 additions & 2 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestLogger(t *testing.T) {
assert.Equal(t, "[INFO] test: this is test: who=programmer why=[testing, dev, 1, 5, \"[3 4]\"]\n", rest)
})

t.Run("renders values in slices with quotes if needed", func(t *testing.T) {
t.Run("renders values in slices with quotes", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Expand All @@ -100,7 +100,7 @@ func TestLogger(t *testing.T) {
dataIdx := strings.IndexByte(str, ' ')
rest := str[dataIdx+1:]

assert.Equal(t, "[INFO] test: this is test: who=programmer why=[\"testing & qa\", dev]\n", rest)
assert.Equal(t, "[INFO] test: this is test: who=programmer why=[\"testing & qa\", \"dev\"]\n", rest)
})

t.Run("formats multiline values nicely", func(t *testing.T) {
Expand Down

0 comments on commit c102b1c

Please sign in to comment.