-
Notifications
You must be signed in to change notification settings - Fork 30
/
test.go
84 lines (68 loc) · 2.11 KB
/
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
package clog
// TestLog redirects all the logs to the testing framework logger
type TestLog struct {
t TestLogInterface
}
// TestLogInterface for use with testing.B or testing.T
type TestLogInterface interface {
Log(args ...interface{})
Logf(format string, args ...interface{})
}
// NewTestLog instantiates a new logger redirecting to the test framework logger
func NewTestLog(t TestLogInterface) *TestLog {
return &TestLog{
t: t,
}
}
// SetTestLog install a test logger as the default logger.
// IMPORTANT: don't forget to run ClearTestLog() at the end of the test
func SetTestLog(t TestLogInterface) {
defaultLog = NewTestLog(t)
}
// ClearTestLog at the end of the test otherwise the logger will keep a reference on t
func ClearTestLog() {
defaultLog = &NullLog{}
}
// Debug sends debugging information
func (l *TestLog) Debug(v ...interface{}) {
l.message(DebugLevel, v...)
}
// Debugf sends debugging information
func (l *TestLog) Debugf(format string, v ...interface{}) {
l.messagef(DebugLevel, format, v...)
}
// Info logs some noticeable information
func (l *TestLog) Info(v ...interface{}) {
l.message(InfoLevel, v...)
}
// Infof logs some noticeable information
func (l *TestLog) Infof(format string, v ...interface{}) {
l.messagef(InfoLevel, format, v...)
}
// Warning send some important message to the console
func (l *TestLog) Warning(v ...interface{}) {
l.message(WarningLevel, v...)
}
// Warningf send some important message to the console
func (l *TestLog) Warningf(format string, v ...interface{}) {
l.messagef(WarningLevel, format, v...)
}
// Error sends error information to the console
func (l *TestLog) Error(v ...interface{}) {
l.message(ErrorLevel, v...)
}
// Errorf sends error information to the console
func (l *TestLog) Errorf(format string, v ...interface{}) {
l.messagef(ErrorLevel, format, v...)
}
func (l *TestLog) message(level int, v ...interface{}) {
v = append([]interface{}{getLevelName(level)}, v...)
l.t.Log(v...)
}
func (l *TestLog) messagef(level int, format string, v ...interface{}) {
l.t.Logf(getLevelName(level)+" "+format, v...)
}
// Verify interface
var (
_ Log = &TestLog{}
)