-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathformatter_test.go
54 lines (47 loc) · 1.63 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
package log
import (
"fmt"
"io"
"testing"
)
func TestFormatterParse(t *testing.T) {
var jsons = []string{
`{"time":"2019-07-10T05:35:54.277Z","level":"debug","level":"error","caller":"pretty.go:42","error":"这是一个🌐哦\n","foo":"bar","n":42,"t":true,"f":false,"o":null,"a":[1,2,3],"obj":{"a":[1,2], "b":{"c":3}},"message":"hello json console color writer\t123"}`,
`{"ts":1234567890,"level":"info","caller":"pretty.go:42","foo":"bad value","foo":"haha","message":"hello self-define time field\t\n"}`,
}
for _, s := range jsons {
var args FormatterArgs
parseFormatterArgs([]byte(s), &args)
t.Logf("%+v", args)
t.Logf("foo=%v", args.Get("foo"))
}
}
func TestFormatterArgsParse(t *testing.T) {
timestamp := "2019-07-10T05:35:54.277Z"
level := "debug"
msg := "hello json console color writer\t123"
category := "cat1"
var json = `{"time":"` + timestamp + `","level":"` + level + `","category":"` + category + `","message":"` + msg + `"}`
var args FormatterArgs
parseFormatterArgs([]byte(json), &args)
if args.Time != timestamp {
t.Fatalf("Failed to parse timestamp: %s != %s", args.Time, timestamp)
}
if args.Level != level {
t.Fatalf("Failed to parse level: %s != %s", args.Level, level)
}
if args.Category != category {
t.Fatalf("Failed to parse category: %s != %s", args.Category, category)
}
if args.Message != msg {
t.Fatalf("Failed to parse messae: %s != %s", args.Message, msg)
}
}
func TestFormatterDefault(t *testing.T) {
DefaultLogger.Writer = &ConsoleWriter{
Formatter: func(w io.Writer, a *FormatterArgs) (int, error) {
return fmt.Fprintf(w, "%s\n", a.Message)
},
}
Info().Msg("aaaa 'b' cccc")
}