Skip to content

Commit

Permalink
Render empty strings with raw=true
Browse files Browse the repository at this point in the history
  • Loading branch information
angrycub committed Nov 30, 2021
1 parent e639bfe commit f780e74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string,
val = st
if st == "" {
val = `""`
raw = true
}
case int:
val = strconv.FormatInt(int64(st), 10)
Expand Down
17 changes: 17 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,23 @@ func TestLogger(t *testing.T) {
assert.Equal(t, `[INFO] test: this is test: who=programmer why="this is \"quoted\""`+"\n", rest)
})

t.Run("prints empty double quotes for empty strings", func(t *testing.T) {
var buf bytes.Buffer

logger := New(&LoggerOptions{
Name: "test",
Output: &buf,
})

logger.Info("this is test", "who", "programmer", "why", ``)

str := buf.String()
dataIdx := strings.IndexByte(str, ' ')
rest := str[dataIdx+1:]

assert.Equal(t, `[INFO] test: this is test: who=programmer why=""`+"\n", rest)
})

t.Run("quotes when there are nonprintable sequences in a value", func(t *testing.T) {
var buf bytes.Buffer

Expand Down

0 comments on commit f780e74

Please sign in to comment.