Skip to content

Commit 2a06ec9

Browse files
author
Evan Phoenix
authored
Merge pull request #116 from hashicorp/f-no-colon
Omit empty colon when message is empty. Fixes #109
2 parents 2c83f91 + 78f517c commit 2a06ec9

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

intlogger.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,14 @@ func (l *intLogger) logPlain(t time.Time, name string, level Level, msg string,
269269

270270
if name != "" {
271271
l.writer.WriteString(name)
272-
l.writer.WriteString(": ")
272+
if msg != "" {
273+
l.writer.WriteString(": ")
274+
l.writer.WriteString(msg)
275+
}
276+
} else if msg != "" {
277+
l.writer.WriteString(msg)
273278
}
274279

275-
l.writer.WriteString(msg)
276-
277280
args = append(l.implied, args...)
278281

279282
var stacktrace CapturedStacktrace

logger_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,24 @@ func TestLogger_JSON(t *testing.T) {
10041004
assert.Equal(t, "this is test", raw["@message"])
10051005
assert.Equal(t, errJsonUnsupportedTypeMsg, raw["@warn"])
10061006
})
1007+
1008+
t.Run("omits the entry for the message when empty", func(t *testing.T) {
1009+
var buf bytes.Buffer
1010+
DefaultOutput = &buf
1011+
1012+
logger := New(&LoggerOptions{
1013+
Name: "test",
1014+
})
1015+
1016+
logger.Info("", "who", "programmer", "why", "testing")
1017+
1018+
str := buf.String()
1019+
dataIdx := strings.IndexByte(str, ' ')
1020+
rest := str[dataIdx+1:]
1021+
1022+
assert.Equal(t, "[INFO] test: who=programmer why=testing\n", rest)
1023+
})
1024+
10071025
}
10081026

10091027
type customErrJSON struct {

0 commit comments

Comments
 (0)