Skip to content

Commit

Permalink
Merge pull request #116 from hashicorp/f-no-colon
Browse files Browse the repository at this point in the history
Omit empty colon when message is empty. Fixes #109
  • Loading branch information
evanphx committed Jul 25, 2022
2 parents 2c83f91 + 78f517c commit 2a06ec9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions intlogger.go
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
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 2a06ec9

Please sign in to comment.