-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_test.go
210 lines (174 loc) · 5.8 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
package log
import (
"fmt"
"sync"
"testing"
"github.com/pingcap/errors"
"github.com/romberli/go-multierror"
"github.com/stretchr/testify/assert"
)
func newRoutine(t *testing.T, wg *sync.WaitGroup) {
defer wg.Done()
t.Log("new routine test")
Debug("this is new routine debug message")
Info("this is new routine info message")
Warn("this is new routine warn message")
Errorf("this is new routine error message.\n%s", "errorf")
}
func TestLog(t *testing.T) {
var (
err error
logConfig *Config
)
wg := &sync.WaitGroup{}
asst := assert.New(t)
level := "info"
format := "text"
fileName := "/tmp/run.log.crn"
maxSize := 1
maxDays := 1
maxBackups := 2
// init logger
t.Log("==========init logger started==========")
asst.Nil(err, "init file log config failed")
logConfig, err = NewConfigWithFileLog(fileName, level, format, maxSize, maxDays, maxBackups, DefaultRotateOption)
if err != nil {
fmt.Printf("got error when creating log config.\n%s", err.Error())
}
MyLogger, MyProps, err = InitLoggerWithConfig(logConfig)
SetDisableEscape(true)
asst.Nil(err, "init logger failed")
t.Log("==========init logger completed==========\n")
// print log
t.Log("==========print main log entry started==========")
Debug("this is main debug message")
Info("this is main \ninfo message")
Infof("this is main info message %s", "infof")
Errorf("this main error message %s", "errorf")
MyLogger.Warn("this is mylogger main warn message")
MyLogger.Warnf("this is mylogger main warn message %s", "warnf")
MyLogger.Error("this is main mylogger error message")
MyLogger.Errorf("this is mylogger main error message %s", "errorf")
// MyLogger.Fatal("this is main mylogger fatal message")
// MyLogger.Fatalf("this is mylogger main fatal message %s", "fatalf")
t.Log("==========print main log entry completed==========")
t.Log("==========print goroutine log entry started==========")
SetTimeFormat(TimeFormatMilliSecond)
SetSeperator(DefaultLogSeparator)
SetDisableDoubleQuotes(true)
// SetDisableEscape(true)
wg.Add(1)
go func() {
defer wg.Done()
t.Log("goroutine test")
Debug("this is goroutine debug message")
// MyLogger.SetDisableDoubleQuotes(false)
MyLogger.Info("this is goroutine mylogger info message")
MyLogger.Warn("this is goroutine mylogger warn message")
Info("this is goroutine info message")
}()
wg.Add(1)
go newRoutine(t, wg)
wg.Wait()
t.Log("==========print goroutine log entry completed==========")
wg.Wait()
t.Log("==========test clone==========")
CloneStdoutLogger().Info("this is cloned logger info message")
CloneStdoutLogger().Infof("this is cloned logger infof message")
Info("this is original logger info message, which should not be printed to console")
t.Log("==========test clone==========")
t.Log("==========add stdout to logger started==========")
MyLogger.CloneAndAddWriteSyncer(NewStdoutWriteSyncer()).Info("test CloneAndAddWriteSyncer()")
MyLogger.Info("mylogger info message after test CloneAndAddWriteSyncer, this should not be printed to console")
stdoutSyncer := NewStdoutWriteSyncer()
AddWriteSyncer(stdoutSyncer)
Info("this is main info message which prints to stdout")
Error("this is main error message which prints to stdout")
MyLogger.Error("this is main mylogger error message which prints to stdout")
t.Log("==========add stdout to logger completed==========")
}
func funcA() error {
return errors.New("function error")
}
func funcB() error {
return errors.Trace(funcA())
}
func funcC() error {
return errors.Trace(funcB())
}
type ErrMessage struct {
Raw string
}
func (em *ErrMessage) Error() string {
return em.Raw
}
func TestLogStack(t *testing.T) {
SetDisableEscape(true)
SetDisableDoubleQuotes(true)
err := funcC()
merr := &multierror.Error{}
merr = multierror.Append(merr, err)
// MyLogger = MyLogger.WithOptions(zap.AddCaller())
// MyLogger.Error(fmt.Sprintf("ttt: %s", err.Error()))
// MyLogger.Error("func: ", zap.Error(err))
// err = JSONMarshal()
// msg := fmt.Sprintf("got error. err:\n%+v", err)
// t.Log(msg)
em := &ErrMessage{fmt.Sprintf("%+v", err)}
MyLogger.Errorf("json: %s", em.Error())
//
// MyLogger.Errorf("json: %+v", merr.WrappedErrors())
// t.Log("=======")
//
// err = fmt.Errorf("wrapped error. error:\n%+v", err)
// MyLogger.Errorf("json: %+v", err)
// Error(err.Error())
}
func TestLogRotate(t *testing.T) {
asst := assert.New(t)
level := "info"
format := "text"
fileName := "/tmp/run.log.current"
maxSize := 1
maxDays := 1
maxBackups := 2
logConfig, err := NewConfigWithFileLog(fileName, level, format, maxSize, maxDays, maxBackups, DefaultRotateOption)
if err != nil {
fmt.Printf("got error when creating log config.\n%s", err.Error())
}
MyLogger, MyProps, err = InitLoggerWithConfig(logConfig)
SetDisableEscape(true)
MyLogger.Info("before rotate")
err = MyLogger.Rotate()
asst.Nil(err, "rotate failed")
MyLogger.Info("after rotate")
}
func TestGlobalLogger(t *testing.T) {
level := "info"
format := "text"
fileName := "/tmp/run.log.current"
maxSize := 1
maxDays := 1
maxBackups := 2
logConfig, err := NewConfigWithFileLog(fileName, level, format, maxSize, maxDays, maxBackups, DefaultRotateOption)
if err != nil {
fmt.Printf("got error when creating log config.\n%s", err.Error())
}
MyLogger, MyProps, err = InitLoggerWithConfig(logConfig)
SetDisableEscape(true)
SetDisableDoubleQuotes(true)
ReplaceGlobals(MyLogger, MyProps)
Info("========")
Info("info message")
Infof("infof message")
AddWriteSyncer(NewStdoutWriteSyncer())
Info("info message after add stdout")
Infof("infof message after add stdout")
Debugf("debugf message before set level to debug")
SetLevel(DebugLevel)
Debug("debug message after set level to debug")
Debugf("debugf message after set level to debug")
SetLevel(WarnLevel)
Debug("debug message after set level to warn")
Errorf("errorf message after set level to warn")
}