-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathformatter_test.go
144 lines (115 loc) · 3.41 KB
/
formatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package slog_test
import (
"fmt"
"runtime"
"strings"
"testing"
"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/dump"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/slog"
"github.com/gookit/slog/handler"
)
func TestFormattableTrait_Formatter(t *testing.T) {
ft := &slog.FormattableTrait{}
tf := slog.AsTextFormatter(ft.Formatter())
assert.NotNil(t, tf)
assert.Panics(t, func() {
slog.AsJSONFormatter(ft.Formatter())
})
ft.SetFormatter(slog.NewJSONFormatter())
jf := slog.AsJSONFormatter(ft.Formatter())
assert.NotNil(t, jf)
assert.Panics(t, func() {
slog.AsTextFormatter(ft.Formatter())
})
}
func TestFormattable_Format(t *testing.T) {
r := newLogRecord("TEST_LOG_MESSAGE format")
f := &slog.FormattableTrait{}
assert.Eq(t, "slog: TEST_LOG_MESSAGE format", r.GoString())
bts, err := f.Format(r)
assert.NoErr(t, err)
str := string(bts)
assert.Contains(t, str, "TEST_LOG_MESSAGE format")
fn := slog.FormatterFunc(func(r *slog.Record) ([]byte, error) {
return []byte(r.Message), nil
})
bts, err = fn.Format(r)
assert.NoErr(t, err)
str = string(bts)
assert.Contains(t, str, "TEST_LOG_MESSAGE format")
}
func TestNewTextFormatter(t *testing.T) {
f := slog.NewTextFormatter()
dump.Println(f.Fields())
assert.Contains(t, f.Fields(), "datetime")
assert.Len(t, f.Fields(), strings.Count(slog.DefaultTemplate, "{{"))
f.SetTemplate(slog.NamedTemplate)
dump.Println(f.Fields())
assert.Contains(t, f.Fields(), "datetime")
assert.Len(t, f.Fields(), strings.Count(slog.NamedTemplate, "{{"))
f.WithEnableColor(true)
assert.True(t, f.EnableColor)
t.Run("CallerFormatFunc", func(t *testing.T) {
buf := byteutil.NewBuffer()
h := handler.IOWriterWithMaxLevel(buf, slog.DebugLevel)
h.SetFormatter(slog.TextFormatterWith(func(f *slog.TextFormatter) {
f.CallerFormatFunc = func(rf *runtime.Frame) string {
return "custom_caller"
}
}))
l := slog.NewWithHandlers(h)
l.Debug("test message")
assert.Contains(t, buf.String(), "custom_caller")
})
}
func TestTextFormatter_Format(t *testing.T) {
r := newLogRecord("TEST_LOG_MESSAGE")
f := slog.NewTextFormatter()
bs, err := f.Format(r)
logTxt := string(bs)
fmt.Println(f.Template(), logTxt)
assert.NoErr(t, err)
assert.NotEmpty(t, logTxt)
assert.NotContains(t, logTxt, "{{")
assert.NotContains(t, logTxt, "}}")
}
func TestNewJSONFormatter(t *testing.T) {
f := slog.NewJSONFormatter()
f.AddField(slog.FieldKeyTimestamp)
h := handler.ConsoleWithLevels(slog.AllLevels)
h.SetFormatter(f)
l := slog.NewWithHandlers(h)
fields := slog.M{
"field1": 123,
"field2": "abc",
"message": "field name is same of message", // will be as fields.message
}
l.WithFields(fields).Info("info", "message")
t.Run("CallerFormatFunc", func(t *testing.T) {
h.SetFormatter(slog.NewJSONFormatter(func(f *slog.JSONFormatter) {
f.CallerFormatFunc = func(rf *runtime.Frame) string {
return rf.Function
}
}))
l.WithFields(fields).Info("info", "message")
})
// PrettyPrint=true
t.Run("PrettyPrint", func(t *testing.T) {
l = slog.New()
h = handler.ConsoleWithMaxLevel(slog.DebugLevel)
f = slog.NewJSONFormatter(func(f *slog.JSONFormatter) {
f.Aliases = slog.StringMap{
"level": "levelName",
}
f.PrettyPrint = true
})
h.SetFormatter(f)
l.AddHandler(h)
l.WithFields(fields).
SetData(slog.M{"key1": "val1"}).
SetExtra(slog.M{"ext1": "val1"}).
Info("info message and PrettyPrint is TRUE")
})
}