Skip to content

Commit

Permalink
Omit empty colon when message is empty. Fixes #109
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Apr 21, 2022
1 parent b6b5567 commit 78f517c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions intlogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,14 @@ func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string,

if name != "" {
l.writer.WriteString(name)
l.writer.WriteString(": ")
if msg != "" {
l.writer.WriteString(": ")
l.writer.WriteString(msg)
}
} else if msg != "" {
l.writer.WriteString(msg)
}

l.writer.WriteString(msg)

args = append(l.implied, args...)

var stacktrace CapturedStacktrace
Expand Down
18 changes: 18 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,24 @@ func TestLogger_JSON(t *testing.T) {
assert.Equal(t, "this is test", raw["@message"])
assert.Equal(t, errJsonUnsupportedTypeMsg, raw["@warn"])
})

t.Run("omits the entry for the message when empty", func(t *testing.T) {
var buf bytes.Buffer
DefaultOutput = &buf

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

logger.Info("", "who", "programmer", "why", "testing")

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

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

}

type customErrJSON struct {
Expand Down

0 comments on commit 78f517c

Please sign in to comment.