forked from trpc-group/trpc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
257 lines (222 loc) · 6.86 KB
/
log_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Tencent is pleased to support the open source community by making tRPC available.
// Copyright (C) 2023 THL A29 Limited, a Tencent company. All rights reserved.
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License that can be found in the LICENSE file.
package log_test
import (
"bytes"
"context"
"fmt"
"path/filepath"
"runtime"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"trpc.group/trpc-go/trpc-go"
"trpc.group/trpc-go/trpc-go/codec"
"trpc.group/trpc-go/trpc-go/log"
)
func TestSetLevel(t *testing.T) {
const level = "0"
log.SetLevel(level, log.LevelInfo)
require.Equal(t, log.LevelInfo, log.GetLevel(level))
}
func TestSetLogger(t *testing.T) {
logger := log.NewZapLog(log.Config{})
log.SetLogger(logger)
require.Equal(t, log.GetDefaultLogger(), logger)
}
func TestLogXXX(t *testing.T) {
log.Fatal("xxx")
}
func TestLoggerNil(t *testing.T) {
ctx := context.Background()
ctx, msg := codec.WithNewMessage(ctx)
msg.WithLogger((log.Logger)(nil))
log.EnableTrace()
log.TraceContext(ctx, "test")
log.TraceContextf(ctx, "test %s", "log")
log.DebugContext(ctx, "test")
log.DebugContextf(ctx, "test %s", "log")
log.InfoContext(ctx, "test")
log.InfoContextf(ctx, "test %s", "log")
log.ErrorContext(ctx, "test")
log.ErrorContextf(ctx, "test %s", "log")
log.WarnContext(ctx, "test")
log.WarnContextf(ctx, "test %s", "log")
log.FatalContext(ctx, "test")
log.FatalContextf(ctx, "test %s", "log")
l := msg.Logger()
require.Nil(t, l)
}
func TestLoggerZapLogWrapper(t *testing.T) {
ctx := context.Background()
ctx, msg := codec.WithNewMessage(ctx)
msg.WithLogger(log.NewZapLog(defaultConfig))
log.EnableTrace()
log.TraceContext(ctx, "test")
log.TraceContextf(ctx, "test")
log.DebugContext(ctx, "test")
log.DebugContextf(ctx, "test")
log.InfoContext(ctx, "test")
log.InfoContextf(ctx, "test %s", "s")
log.ErrorContext(ctx, "test")
log.ErrorContextf(ctx, "test")
log.WarnContext(ctx, "test")
log.WarnContextf(ctx, "test")
msg.WithLogger(log.NewZapLog(defaultConfig))
log.WithContextFields(ctx, "a", "a")
log.SetLevel("console", log.LevelDebug)
require.Equal(t, log.GetLevel("console"), log.LevelDebug)
}
func TestWithContextFields(t *testing.T) {
ctx := trpc.BackgroundContext()
log.WithContextFields(ctx, "k", "v")
require.NotNil(t, codec.Message(ctx).Logger())
ctx = context.Background()
newCtx := log.WithContextFields(ctx, "k", "v")
require.Nil(t, codec.Message(ctx).Logger())
require.NotNil(t, codec.Message(newCtx).Logger())
}
func TestOptionLogger1(t *testing.T) {
log.Debug("test1")
log.Debug("test2")
log.Debug("test3")
ctx := context.Background()
ctx, msg := codec.WithNewMessage(ctx)
msg.WithCallerServiceName("trpc.test.helloworld.Greeter")
log.WithContextFields(ctx, "a", "a")
log.TraceContext(ctx, "test")
log.InfoContext(ctx, "test")
log.WithContextFields(ctx, "b", "b")
log.InfoContext(ctx, "test")
trpc.Message(ctx).WithLogger(log.Get("default"))
log.DebugContext(ctx, "custom log msg")
}
func TestCustomLogger(t *testing.T) {
log.Register("custom", log.NewZapLogWithCallerSkip(log.Config{log.OutputConfig{Writer: "console"}}, 1))
log.Get("custom").Debug("test")
}
const (
noOptionBufLogger = "noOptionBuf"
customBufLogger = "customBuf"
)
func getCtxFuncs() []func() context.Context {
return []func() context.Context{
func() context.Context {
return context.Background()
},
func() context.Context {
ctx, msg := codec.WithNewMessage(context.Background())
msg.WithCallerServiceName("trpc.test.helloworld.Greeter")
return ctx
}, func() context.Context {
ctx, msg := codec.WithNewMessage(context.Background())
msg.WithLogger(log.GetDefaultLogger())
msg.WithCallerServiceName("trpc.test.helloworld.Greeter")
return ctx
}, func() context.Context {
ctx, msg := codec.WithNewMessage(context.Background())
msg.WithLogger(log.Get(noOptionBufLogger))
msg.WithCallerServiceName("trpc.test.helloworld.Greeter")
return ctx
},
}
}
func TestWithContext(t *testing.T) {
old := log.GetDefaultLogger()
defer log.SetLogger(old)
for _, ctxFunc := range getCtxFuncs() {
ctx := ctxFunc()
checkTrace(t, func() {
log.WithContext(ctx, log.Field{Key: "123", Value: 123}).Debugf("test")
log.WithContext(ctx, log.Field{Key: "123", Value: 123}).With(log.Field{Key: "k2", Value: "v2"}).Debugf("test")
log.WithContext(ctx, log.Field{Key: "123", Value: 123}).With(log.Field{Key: "k2", Value: "v2"}).With(log.Field{Key: "k2", Value: "v2"}).Debugf("test")
}, nil)
}
}
func TestStacktrace(t *testing.T) {
checkTrace(t, func() {
log.Debug("test")
log.Error("test")
}, nil)
}
func check(t *testing.T, out *bytes.Buffer, fn func()) {
fn()
_, file, start, ok := runtime.Caller(2)
assert.True(t, ok)
pathPre := filepath.Join(filepath.Base(filepath.Dir(file)), filepath.Base(file)) + ":"
trace := out.String()
count := strings.Count(trace, pathPre)
fmt.Println(" line count:", count, "start:", start+1, "end:", start+count)
fmt.Println(trace)
for line := start + 1; line <= start+count; line++ {
path := pathPre + strconv.Itoa(line)
require.Contains(t, out.String(), path, "log trace error")
}
verifyNoZap(t, trace)
}
var (
buf = &bytes.Buffer{}
)
func init() {
log.Register(customBufLogger, log.NewZapBufLogger(buf, 1))
log.Register(noOptionBufLogger, newNoOptionBufLogger(buf, 1))
}
// checkTrace set buf log to check trace.
func checkTrace(t *testing.T, fn func(), setLog func()) {
*buf = bytes.Buffer{}
log.SetLogger(log.NewZapBufLogger(buf, 2))
if setLog != nil {
setLog()
}
check(t, buf, fn)
}
func verifyNoZap(t *testing.T, logs string) {
for _, fnPrefix := range zapPackages {
require.NotContains(t, logs, fnPrefix, "should not contain zap package")
}
}
// zapPackages are packages that we search for in the logging output to match a
// zap stack frame.
var zapPackages = []string{
"go.uber.org/zap",
"go.uber.org/zap/zapcore",
}
func TestLogFatal(t *testing.T) {
old := log.GetDefaultLogger()
defer log.SetLogger(old)
var h customWriteHook
log.SetLogger(log.NewZapFatalLogger(&h))
log.Fatal("test")
assert.True(t, h.called)
h.called = false
log.Fatalf("test")
assert.True(t, h.called)
h.called = false
ctx := context.Background()
log.FatalContext(ctx, "test")
assert.True(t, h.called)
h.called = false
log.FatalContextf(ctx, "test")
assert.True(t, h.called)
ctx, msg := codec.WithNewMessage(context.Background())
msg.WithLogger(log.GetDefaultLogger())
msg.WithCallerServiceName("trpc.test.helloworld.Greeter")
h.called = false
log.FatalContext(ctx, "test")
assert.True(t, h.called)
h.called = false
log.FatalContextf(ctx, "test")
assert.True(t, h.called)
}
type customWriteHook struct {
called bool
}
func (h *customWriteHook) OnWrite(_ *zapcore.CheckedEntry, _ []zap.Field) {
h.called = true
}