Skip to content

Commit

Permalink
Merge pull request x-cray#24 from gz-c/fix-double-spaces
Browse files Browse the repository at this point in the history
Fix double space in log line with empty message
  • Loading branch information
cjbassi committed Dec 16, 2018
2 parents e9c19a7 + 41ebb42 commit b87ba5c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 15 additions & 3 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sync"
"time"

"github.com/sirupsen/logrus"
"github.com/mgutz/ansi"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
)

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

if f.DisableTimestamp {
fmt.Fprintf(b, "%s"+ prefixFormat + " " + messageFormat, level, prefix, message)
if message == "" {
fmt.Fprintf(b, "%s%s", level, prefix)
} else {
fmt.Fprintf(b, "%s "+prefixFormat+" "+messageFormat, level, prefix, message)
}
} else {
var timestamp string
if !f.FullTimestamp {
timestamp = fmt.Sprintf("[%04d]", miniTS())
} else {
timestamp = fmt.Sprintf("[%s]", entry.Time.Format(timestampFormat))
}
fmt.Fprintf(b, "%s %s" + prefixFormat + " " + messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message)

coloredTimestamp := colorScheme.TimestampColor(timestamp)

if message == "" {
fmt.Fprintf(b, "%s %s%s", coloredTimestamp, level, prefix)
} else {
fmt.Fprintf(b, "%s %s "+prefixFormat+" "+messageFormat, coloredTimestamp, level, prefix, message)
}
}

for _, k := range keys {
if k != "prefix" {
v := entry.Data[k]
Expand Down
13 changes: 11 additions & 2 deletions formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package prefixed_test
import (
. "github.com/x-cray/logrus-prefixed-formatter"

"github.com/sirupsen/logrus"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
)

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

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

Describe("Formatted output with no message", func() {
It("should not have two consecutive spaces", func() {
formatter.DisableTimestamp = true
formatter.ForceFormatting = true
log.WithFields(logrus.Fields{"animal": "walrus"}).Debug()
Ω(output.GetValue()).Should(Equal("DEBUG animal=walrus\n"))
})
})

Describe("Theming support", func() {

})
Expand Down

0 comments on commit b87ba5c

Please sign in to comment.