Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -269,16 +269,28 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
}

if f.DisableTimestamp {
fmt.Fprintf(b, "%s%s "+messageFormat, level, prefix, message)
if message == "" {
fmt.Fprintf(b, "%s%s", level, prefix)
} else {
fmt.Fprintf(b, "%s%s "+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%s "+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%s "+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