Add custom sorting function in text formatter
This commit is contained in:
+32
-2
@@ -5,11 +5,13 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
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
|
||||
// formatter output.
|
||||
func TestCustomSorting(t *testing.T) {
|
||||
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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user