diff --git a/README.md b/README.md index cf10f27..a7de58e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/formatter.go b/formatter.go index 1235bcc..28377f7 100644 --- a/formatter.go +++ b/formatter.go @@ -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" ) @@ -41,6 +41,7 @@ var ( TimestampColor: ansi.ColorFunc(""), } defaultCompiledColorScheme *compiledColorScheme = compileColorScheme(defaultColorScheme) + defaultPrefixFieldName = "prefix" ) func miniTS() int { @@ -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 @@ -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) @@ -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) } @@ -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 diff --git a/formatter_test.go b/formatter_test.go index 0b185e8..000197e 100644 --- a/formatter_test.go +++ b/formatter_test.go @@ -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() { @@ -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")) }) }) @@ -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")) + }) + }) })