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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func main() {
* `QuoteEmptyFields bool` — wrap empty fields in quotes if true.
* `QuoteCharacter string` — can be set to the override the default quoting character `"` with something else. For example: `'`, or `` ` ``.
* `SpacePadding int` — pad msg field with spaces on the right for display. The value for this parameter will be the size of padding. Its default value is zero, which means no padding will be applied.
* `PrefixFieldName string` - choose custom logrus field name for prefix detection

### Methods

Expand Down
19 changes: 14 additions & 5 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 @@ -41,6 +41,7 @@ var (
TimestampColor: ansi.ColorFunc(""),
}
defaultCompiledColorScheme *compiledColorScheme = compileColorScheme(defaultColorScheme)
defaultPrefixFieldName = "prefix"
)

func miniTS() int {
Expand Down Expand Up @@ -110,6 +111,9 @@ type TextFormatter struct {
// Its default value is zero, which means no padding will be applied for msg.
SpacePadding int

// The field name for the prefix detection if found in logrus.Entry.Data
PrefixFieldName string

// Color scheme to use.
colorScheme *compiledColorScheme

Expand Down Expand Up @@ -253,7 +257,12 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
prefix := ""
message := entry.Message

if prefixValue, ok := entry.Data["prefix"]; ok {
prefixFieldName := f.PrefixFieldName
if prefixFieldName == "" {
prefixFieldName = defaultPrefixFieldName
}

if prefixValue, ok := entry.Data[prefixFieldName]; ok {
prefix = colorScheme.PrefixColor(" " + prefixValue.(string) + ":")
} else {
prefixValue, trimmedMsg := extractPrefix(entry.Message)
Expand All @@ -280,7 +289,7 @@ func (f *TextFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry, keys
fmt.Fprintf(b, "%s %s%s "+messageFormat, colorScheme.TimestampColor(timestamp), level, prefix, message)
}
for _, k := range keys {
if k != "prefix" {
if k != prefixFieldName {
v := entry.Data[k]
fmt.Fprintf(b, " %s=%+v", levelColor(k), v)
}
Expand Down Expand Up @@ -345,12 +354,12 @@ func (f *TextFormatter) appendValue(b *bytes.Buffer, value interface{}) {
// This is to not silently overwrite `time`, `msg` and `level` fields when
// dumping it. If this code wasn't there doing:
//
// logrus.WithField("level", 1).Info("hello")
// logrus.WithField("level", 1).Info("hello")
//
// would just silently drop the user provided level. Instead with this code
// it'll be logged as:
//
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
// {"level": "info", "fields.level": 1, "msg": "hello", "time": "..."}
func prefixFieldClashes(data logrus.Fields) {
if t, ok := data["time"]; ok {
data["fields.time"] = t
Expand Down
14 changes: 12 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 @@ -48,4 +48,14 @@ var _ = Describe("Formatter", func() {
Describe("Theming support", func() {

})

Describe("Custom prefix", func() {
It("should detect custom prefix from log fields", func() {
formatter.DisableTimestamp = true
formatter.PrefixFieldName = "moduleName"
formatter.ForceFormatting = true
log.WithFields(logrus.Fields{"moduleName": "main"}).Debug("something happened")
Ω(output.GetValue()).Should(Equal("DEBUG main: something happened\n"))
})
})
})