-
Notifications
You must be signed in to change notification settings - Fork 26
/
processor_test.go
74 lines (60 loc) · 2 KB
/
processor_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
package slog_test
import (
"context"
"fmt"
"os"
"testing"
"github.com/gookit/goutil/byteutil"
"github.com/gookit/goutil/testutil/assert"
"github.com/gookit/slog"
)
func TestLogger_AddProcessor(t *testing.T) {
buf := new(byteutil.Buffer)
l := slog.NewJSONSugared(buf, slog.InfoLevel)
l.AddProcessor(slog.AddHostname())
l.Info("message")
hostname, _ := os.Hostname()
// {"channel":"application","data":{},"datetime":"2020/07/17 12:01:35","extra":{},"hostname":"InhereMac","level":"INFO","message":"message"}
str := buf.String()
buf.Reset()
assert.Contains(t, str, `"level":"INFO"`)
assert.Contains(t, str, `"message":"message"`)
assert.Contains(t, str, fmt.Sprintf(`"hostname":"%s"`, hostname))
l.ResetProcessors()
l.PushProcessor(slog.MemoryUsage)
l.Info("message2")
// {"channel":"application","data":{},"datetime":"2020/07/16 16:40:18","extra":{"memoryUsage":326072},"level":"INFO","message":"message2"}
str = buf.String()
buf.Reset()
assert.Contains(t, str, `"message":"message2"`)
assert.Contains(t, str, `"memoryUsage":`)
l.ResetProcessors()
l.SetProcessors([]slog.Processor{slog.AddUniqueID("requestId")})
l.Info("message3")
str = buf.String()
buf.Reset()
assert.Contains(t, str, `"message":"message3"`)
assert.Contains(t, str, `"requestId":`)
fmt.Print(str)
l.ResetProcessors()
l.AddProcessors(slog.AppendCtxKeys("traceId", "userId"))
l.Info("message4")
str = buf.ResetAndGet()
fmt.Print(str)
assert.Contains(t, str, `"message":"message4"`)
assert.NotContains(t, str, `"traceId"`)
ctx := context.WithValue(context.Background(), "traceId", "traceId123abc456")
l.WithCtx(ctx).Info("message5")
str = buf.ResetAndGet()
fmt.Print(str)
assert.Contains(t, str, `"message":"message5"`)
assert.Contains(t, str, `"traceId":"traceId123abc456"`)
}
func TestProcessable_AddProcessor(t *testing.T) {
ps := &slog.Processable{}
ps.AddProcessor(slog.MemoryUsage)
r := newLogRecord("error message")
ps.ProcessRecord(r)
assert.NotEmpty(t, r.Extra)
assert.Contains(t, r.Extra, "memoryUsage")
}