-
Notifications
You must be signed in to change notification settings - Fork 29
/
log_test.go
157 lines (132 loc) · 4.28 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
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"bufio"
"bytes"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func TestExport(t *testing.T) {
ts := newTestLogSpy(t)
conf := &Config{Level: "debug", DisableTimestamp: true}
logger, _, _ := InitTestLogger(ts, conf)
ReplaceGlobals(logger, nil)
Info("Testing")
Debug("Testing")
Warn("Testing")
Error("Testing")
ts.assertMessagesContains("log_test.go:")
L().Info("Testing")
ts.assertMessagesContains("log_test.go:")
L().WithOptions(zap.AddCallerSkip(1)).Info("Testing")
ts.assertLastMessageNotContains("log_test.go:")
ts2 := newTestLogSpy(t)
logger2, _, _ := InitTestLogger(ts2, conf)
ReplaceGlobals(logger2, nil)
newLogger := With(zap.String("name", "tester"), zap.Int64("age", 42))
newLogger.Info("hello")
newLogger.Debug("world")
ts2.assertMessagesContains(`name=tester`)
ts2.assertMessagesContains(`age=42`)
ts.assertMessagesNotContains(`name=tester`)
}
func TestReplaceGlobals(t *testing.T) {
ts := newTestLogSpy(t)
conf := &Config{Level: "debug", DisableTimestamp: true}
logger, _, _ := InitTestLogger(ts, conf)
ReplaceGlobals(logger, nil)
Info(`foo_1`)
ts.assertLastMessageContains(`foo_1`)
ts2 := newTestLogSpy(t)
logger2, _, _ := InitTestLogger(ts2, conf)
restoreGlobal := ReplaceGlobals(logger2, nil)
Info(`foo_2`)
ts.assertMessagesNotContains(`foo_2`)
ts.assertLastMessageContains(`foo_1`)
ts2.assertLastMessageContains(`foo_2`)
ts2.assertMessagesNotContains(`foo_1`)
restoreGlobal()
ts.assertMessagesNotContains(`foo_2`)
ts.assertLastMessageContains(`foo_1`)
ts2.assertLastMessageContains(`foo_2`)
ts2.assertMessagesNotContains(`foo_1`)
Info(`foo_3`)
ts.assertMessagesNotContains(`foo_2`)
ts.assertLastMessageContains(`foo_3`)
ts2.assertLastMessageContains(`foo_2`)
ts2.assertMessagesNotContains(`foo_1`)
ts2.assertMessagesNotContains(`foo_3`)
}
func TestZapTextEncoder(t *testing.T) {
conf := &Config{Level: "debug", File: FileLogConfig{}, DisableTimestamp: true}
var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
encoder, err := NewTextEncoder(conf)
require.NoError(t, err)
logger := zap.New(zapcore.NewCore(encoder, zapcore.AddSync(writer), zapcore.InfoLevel)).Sugar()
logger.Info("this is a message from zap")
_ = writer.Flush()
require.Equal(t, `[INFO] ["this is a message from zap"]`+"\n", buffer.String())
}
func TestRegisteredTextEncoder(t *testing.T) {
sink := &testingSink{new(bytes.Buffer)}
_ = zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {
return sink, nil
})
lgc := zap.NewProductionConfig()
lgc.Encoding = ZapEncodingName
lgc.OutputPaths = []string{"memory://"}
lg, err := lgc.Build()
require.Nil(t, err)
lg.Info("this is a message from zap")
require.Contains(t, sink.String(), `["this is a message from zap"]`)
}
// testingSink implements zap.Sink by writing all messages to a buffer.
type testingSink struct {
*bytes.Buffer
}
// Implement Close and Sync as no-ops to satisfy the interface. The Write
// method is provided by the embedded buffer.
func (s *testingSink) Close() error { return nil }
func (s *testingSink) Sync() error { return nil }
func TestTimeout(t *testing.T) {
sink := &testingSink{new(bytes.Buffer)}
ws := LockWithTimeout(sink, 3)
ws.Write([]byte("abc"))
ws.Sync()
require.Contains(t, sink.String(), `abc`)
var h hang
ws = LockWithTimeout(zapcore.AddSync(h), 3)
panicCh := make(chan struct{}, 2)
for i := 0; i < 2; i++ {
go func() {
defer func() {
if x := recover(); x != nil {
panicCh <- struct{}{}
}
}()
ws.Write([]byte("abc")) // This should not make the caller hang
}()
}
<-panicCh
}
type hang struct{}
func (_ hang) Write(_ []byte) (int, error) {
<-make(chan struct{}) // block forever
return 0, nil
}