mirror of https://github.com/sirupsen/logrus.git
Merge pull request #677 from neilisaac/text-field-map
Add FieldMap support to TextFormatter
This commit is contained in:
commit
e3292c4c4d
|
@ -34,15 +34,18 @@ func prefixFieldClashes(data Fields, fieldMap FieldMap) {
|
||||||
timeKey := fieldMap.resolve(FieldKeyTime)
|
timeKey := fieldMap.resolve(FieldKeyTime)
|
||||||
if t, ok := data[timeKey]; ok {
|
if t, ok := data[timeKey]; ok {
|
||||||
data["fields."+timeKey] = t
|
data["fields."+timeKey] = t
|
||||||
|
delete(data, timeKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
msgKey := fieldMap.resolve(FieldKeyMsg)
|
msgKey := fieldMap.resolve(FieldKeyMsg)
|
||||||
if m, ok := data[msgKey]; ok {
|
if m, ok := data[msgKey]; ok {
|
||||||
data["fields."+msgKey] = m
|
data["fields."+msgKey] = m
|
||||||
|
delete(data, msgKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
levelKey := fieldMap.resolve(FieldKeyLevel)
|
levelKey := fieldMap.resolve(FieldKeyLevel)
|
||||||
if l, ok := data[levelKey]; ok {
|
if l, ok := data[levelKey]; ok {
|
||||||
data["fields."+levelKey] = l
|
data["fields."+levelKey] = l
|
||||||
|
delete(data, levelKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,7 +51,6 @@ type TextFormatter struct {
|
||||||
// be desired.
|
// be desired.
|
||||||
DisableSorting bool
|
DisableSorting bool
|
||||||
|
|
||||||
|
|
||||||
// Disables the truncation of the level text to 4 characters.
|
// Disables the truncation of the level text to 4 characters.
|
||||||
DisableLevelTruncation bool
|
DisableLevelTruncation bool
|
||||||
|
|
||||||
|
@ -61,6 +60,15 @@ type TextFormatter struct {
|
||||||
// Whether the logger's out is to a terminal
|
// Whether the logger's out is to a terminal
|
||||||
isTerminal bool
|
isTerminal bool
|
||||||
|
|
||||||
|
// FieldMap allows users to customize the names of keys for default fields.
|
||||||
|
// As an example:
|
||||||
|
// formatter := &TextFormatter{
|
||||||
|
// FieldMap: FieldMap{
|
||||||
|
// FieldKeyTime: "@timestamp",
|
||||||
|
// FieldKeyLevel: "@level",
|
||||||
|
// FieldKeyMsg: "@message"}}
|
||||||
|
FieldMap FieldMap
|
||||||
|
|
||||||
sync.Once
|
sync.Once
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +80,8 @@ func (f *TextFormatter) init(entry *Entry) {
|
||||||
|
|
||||||
// Format renders a single log entry
|
// Format renders a single log entry
|
||||||
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
var b *bytes.Buffer
|
prefixFieldClashes(entry.Data, f.FieldMap)
|
||||||
|
|
||||||
keys := make([]string, 0, len(entry.Data))
|
keys := make([]string, 0, len(entry.Data))
|
||||||
for k := range entry.Data {
|
for k := range entry.Data {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
|
@ -81,14 +90,14 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
if !f.DisableSorting {
|
if !f.DisableSorting {
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var b *bytes.Buffer
|
||||||
if entry.Buffer != nil {
|
if entry.Buffer != nil {
|
||||||
b = entry.Buffer
|
b = entry.Buffer
|
||||||
} else {
|
} else {
|
||||||
b = &bytes.Buffer{}
|
b = &bytes.Buffer{}
|
||||||
}
|
}
|
||||||
|
|
||||||
prefixFieldClashes(entry.Data, emptyFieldMap)
|
|
||||||
|
|
||||||
f.Do(func() { f.init(entry) })
|
f.Do(func() { f.init(entry) })
|
||||||
|
|
||||||
isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors
|
isColored := (f.ForceColors || f.isTerminal) && !f.DisableColors
|
||||||
|
@ -101,11 +110,11 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
|
||||||
f.printColored(b, entry, keys, timestampFormat)
|
f.printColored(b, entry, keys, timestampFormat)
|
||||||
} else {
|
} else {
|
||||||
if !f.DisableTimestamp {
|
if !f.DisableTimestamp {
|
||||||
f.appendKeyValue(b, "time", entry.Time.Format(timestampFormat))
|
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyTime), entry.Time.Format(timestampFormat))
|
||||||
}
|
}
|
||||||
f.appendKeyValue(b, "level", entry.Level.String())
|
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyLevel), entry.Level.String())
|
||||||
if entry.Message != "" {
|
if entry.Message != "" {
|
||||||
f.appendKeyValue(b, "msg", entry.Message)
|
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyMsg), entry.Message)
|
||||||
}
|
}
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
f.appendKeyValue(b, key, entry.Data[key])
|
f.appendKeyValue(b, key, entry.Data[key])
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFormatting(t *testing.T) {
|
func TestFormatting(t *testing.T) {
|
||||||
|
@ -175,5 +177,44 @@ func TestDisableTimestampWithColoredOutput(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTextFormatterFieldMap(t *testing.T) {
|
||||||
|
formatter := &TextFormatter{
|
||||||
|
DisableColors: true,
|
||||||
|
FieldMap: FieldMap{
|
||||||
|
FieldKeyMsg: "message",
|
||||||
|
FieldKeyLevel: "somelevel",
|
||||||
|
FieldKeyTime: "timeywimey",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
entry := &Entry{
|
||||||
|
Message: "oh hi",
|
||||||
|
Level: WarnLevel,
|
||||||
|
Time: time.Date(1981, time.February, 24, 4, 28, 3, 100, time.UTC),
|
||||||
|
Data: Fields{
|
||||||
|
"field1": "f1",
|
||||||
|
"message": "messagefield",
|
||||||
|
"somelevel": "levelfield",
|
||||||
|
"timeywimey": "timeywimeyfield",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := formatter.Format(entry)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal("Unable to format entry: ", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t,
|
||||||
|
`timeywimey="1981-02-24T04:28:03Z" `+
|
||||||
|
`somelevel=warning `+
|
||||||
|
`message="oh hi" `+
|
||||||
|
`field1=f1 `+
|
||||||
|
`fields.message=messagefield `+
|
||||||
|
`fields.somelevel=levelfield `+
|
||||||
|
`fields.timeywimey=timeywimeyfield`+"\n",
|
||||||
|
string(b),
|
||||||
|
"Formatted output doesn't respect FieldMap")
|
||||||
|
}
|
||||||
|
|
||||||
// TODO add tests for sorting etc., this requires a parser for the text
|
// TODO add tests for sorting etc., this requires a parser for the text
|
||||||
// formatter output.
|
// formatter output.
|
||||||
|
|
Loading…
Reference in New Issue