Add custom sorting function in text formatter

This commit is contained in:
David Bariod 2018-09-25 13:45:23 +02:00
parent 5a88d3c21d
commit 73bc94e60c
2 changed files with 72 additions and 14 deletions

View File

@ -55,6 +55,9 @@ type TextFormatter struct {
// be desired. // be desired.
DisableSorting bool DisableSorting bool
// The keys sorting function, when uninitialized it uses sort.Strings.
SortingFunc func([]string)
// Disables the truncation of the level text to 4 characters. // Disables the truncation of the level text to 4 characters.
DisableLevelTruncation bool DisableLevelTruncation bool
@ -73,7 +76,7 @@ type TextFormatter struct {
// FieldKeyMsg: "@message"}} // FieldKeyMsg: "@message"}}
FieldMap FieldMap FieldMap FieldMap
sync.Once terminalInitOnce sync.Once
} }
func (f *TextFormatter) init(entry *Entry) { func (f *TextFormatter) init(entry *Entry) {
@ -111,8 +114,29 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
keys = append(keys, k) keys = append(keys, k)
} }
fixedKeys := make([]string, 0, 3+len(entry.Data))
if !f.DisableTimestamp {
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyTime))
}
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyLevel))
if entry.Message != "" {
fixedKeys = append(fixedKeys, f.FieldMap.resolve(FieldKeyMsg))
}
if !f.DisableSorting { if !f.DisableSorting {
if f.SortingFunc == nil {
sort.Strings(keys) sort.Strings(keys)
fixedKeys = append(fixedKeys, keys...)
} else {
if !f.isColored() {
fixedKeys = append(fixedKeys, keys...)
f.SortingFunc(fixedKeys)
} else {
f.SortingFunc(keys)
}
}
} else {
fixedKeys = append(fixedKeys, keys...)
} }
var b *bytes.Buffer var b *bytes.Buffer
@ -122,7 +146,7 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
b = &bytes.Buffer{} b = &bytes.Buffer{}
} }
f.Do(func() { f.init(entry) }) f.terminalInitOnce.Do(func() { f.init(entry) })
timestampFormat := f.TimestampFormat timestampFormat := f.TimestampFormat
if timestampFormat == "" { if timestampFormat == "" {
@ -131,15 +155,19 @@ func (f *TextFormatter) Format(entry *Entry) ([]byte, error) {
if f.isColored() { if f.isColored() {
f.printColored(b, entry, keys, timestampFormat) f.printColored(b, entry, keys, timestampFormat)
} else { } else {
if !f.DisableTimestamp { for _, key := range fixedKeys {
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyTime), entry.Time.Format(timestampFormat)) var value interface{}
switch key {
case f.FieldMap.resolve(FieldKeyTime):
value = entry.Time.Format(timestampFormat)
case f.FieldMap.resolve(FieldKeyLevel):
value = entry.Level.String()
case f.FieldMap.resolve(FieldKeyMsg):
value = entry.Message
default:
value = entry.Data[key]
} }
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyLevel), entry.Level.String()) f.appendKeyValue(b, key, value)
if entry.Message != "" {
f.appendKeyValue(b, f.FieldMap.resolve(FieldKeyMsg), entry.Message)
}
for _, key := range keys {
f.appendKeyValue(b, key, entry.Data[key])
} }
} }

View File

@ -5,11 +5,13 @@ import (
"errors" "errors"
"fmt" "fmt"
"os" "os"
"sort"
"strings" "strings"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestFormatting(t *testing.T) { func TestFormatting(t *testing.T) {
@ -446,5 +448,33 @@ func TestTextFormatterIsColored(t *testing.T) {
} }
} }
// TODO add tests for sorting etc., this requires a parser for the text func TestCustomSorting(t *testing.T) {
// formatter output. formatter := &TextFormatter{
DisableColors: true,
SortingFunc: func(keys []string) {
sort.Slice(keys, func(i, j int) bool {
if keys[j] == "prefix" {
return false
}
if keys[i] == "prefix" {
return true
}
return strings.Compare(keys[i], keys[j]) == -1
})
},
}
entry := &Entry{
Message: "Testing custom sort function",
Time: time.Now(),
Level: InfoLevel,
Data: Fields{
"test": "testvalue",
"prefix": "the application prefix",
"blablabla": "blablabla",
},
}
b, err := formatter.Format(entry)
require.NoError(t, err)
require.True(t, strings.HasPrefix(string(b), "prefix="), "format output is %q", string(b))
}