Skip to content

Commit c102b1c

Browse files
committed
quote strings in slices to reduce reading confusion
1 parent 7702d38 commit c102b1c

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-11
lines changed

interceptlogger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func TestInterceptLogger(t *testing.T) {
234234
output := buf.String()
235235
dataIdx := strings.IndexByte(output, ' ')
236236
rest := output[dataIdx+1:]
237-
assert.Equal(t, "[DEBUG] with_test.sub_logger.http: test1: parent=logger path=/some/test/path args=[test, test]\n", rest)
237+
assert.Equal(t, "[DEBUG] with_test.sub_logger.http: test1: parent=logger path=/some/test/path args=[\"test\", \"test\"]\n", rest)
238238
})
239239

240240
t.Run("derived standard loggers send output to sinks", func(t *testing.T) {

intlogger.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,22 +379,19 @@ func (l *intLogger) renderSlice(v reflect.Value) string {
379379

380380
switch sv.Kind() {
381381
case reflect.String:
382-
val = sv.String()
382+
val = strconv.Quote(sv.String())
383383
case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64:
384384
val = strconv.FormatInt(sv.Int(), 10)
385385
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
386386
val = strconv.FormatUint(sv.Uint(), 10)
387387
default:
388388
val = fmt.Sprintf("%v", sv.Interface())
389+
if strings.ContainsAny(val, " \t\n\r") {
390+
val = strconv.Quote(val)
391+
}
389392
}
390393

391-
if strings.ContainsAny(val, " \t\n\r") {
392-
buf.WriteByte('"')
393-
buf.WriteString(val)
394-
buf.WriteByte('"')
395-
} else {
396-
buf.WriteString(val)
397-
}
394+
buf.WriteString(val)
398395
}
399396

400397
buf.WriteRune(']')

logger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func TestLogger(t *testing.T) {
8686
assert.Equal(t, "[INFO] test: this is test: who=programmer why=[testing, dev, 1, 5, \"[3 4]\"]\n", rest)
8787
})
8888

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

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

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

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

0 commit comments

Comments
 (0)