Skip to content

Commit d9c1e8f

Browse files
committed
Merge pull request x-cray#24 from gz-c/fix-double-spaces
Fix double space in log line with empty message
2 parents e9c19a7 + 41ebb42 commit d9c1e8f

2 files changed

Lines changed: 26 additions & 5 deletions

File tree

formatter.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"sync"
1212
"time"
1313

14-
"github.com/sirupsen/logrus"
1514
"github.com/mgutz/ansi"
15+
"github.com/sirupsen/logrus"
1616
"golang.org/x/crypto/ssh/terminal"
1717
)
1818

@@ -285,16 +285,28 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
285285
}
286286

287287
if f.DisableTimestamp {
288-
fmt.Fprintf(b, "%s"+ prefixFormat + " " + messageFormat, level, prefix, message)
288+
if message == "" {
289+
fmt.Fprintf(b, "%s%s", level, prefix)
290+
} else {
291+
fmt.Fprintf(b, "%s%s "+prefixFormat+" "+messageFormat, level, prefix, message)
292+
}
289293
} else {
290294
var timestamp string
291295
if !f.FullTimestamp {
292296
timestamp = fmt.Sprintf("[%04d]", miniTS())
293297
} else {
294298
timestamp = fmt.Sprintf("[%s]", entry.Time.Format(timestampFormat))
295299
}
296-
fmt.Fprintf(b, "%s %s" + prefixFormat + " " + messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message)
300+
301+
coloredTimestamp := colorScheme.TimestampColor(timestamp)
302+
303+
if message == "" {
304+
fmt.Fprintf(b, "%s %s%s", coloredTimestamp, level, prefix)
305+
} else {
306+
fmt.Fprintf(b, "%s %s%s "+prefixFormat+" "+messageFormat, coloredTimestamp, level, prefix, message)
307+
}
297308
}
309+
298310
for _, k := range keys {
299311
if k != "prefix" {
300312
v := entry.Data[k]

formatter_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package prefixed_test
33
import (
44
. "github.com/x-cray/logrus-prefixed-formatter"
55

6-
"github.com/sirupsen/logrus"
76
. "github.com/onsi/ginkgo"
87
. "github.com/onsi/gomega"
8+
"github.com/sirupsen/logrus"
99
)
1010

1111
var _ = Describe("Formatter", func() {
@@ -31,7 +31,7 @@ var _ = Describe("Formatter", func() {
3131

3232
It("should output message with additional field", func() {
3333
formatter.DisableTimestamp = true
34-
log.WithFields(logrus.Fields{ "animal": "walrus" }).Debug("test")
34+
log.WithFields(logrus.Fields{"animal": "walrus"}).Debug("test")
3535
Ω(output.GetValue()).Should(Equal("level=debug msg=test animal=walrus\n"))
3636
})
3737
})
@@ -45,6 +45,15 @@ var _ = Describe("Formatter", func() {
4545
})
4646
})
4747

48+
Describe("Formatted output with no message", func() {
49+
It("should not have two consecutive spaces", func() {
50+
formatter.DisableTimestamp = true
51+
formatter.ForceFormatting = true
52+
log.WithFields(logrus.Fields{"animal": "walrus"}).Debug()
53+
Ω(output.GetValue()).Should(Equal("DEBUG animal=walrus\n"))
54+
})
55+
})
56+
4857
Describe("Theming support", func() {
4958

5059
})

0 commit comments

Comments
 (0)