-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathstdlog_test.go
80 lines (68 loc) · 1.95 KB
/
stdlog_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
package nxlog4go
import (
"bytes"
"log"
"testing"
)
func TestNewStdLog(t *testing.T) {
buf := new(bytes.Buffer)
logger := New(buf, "redirected", Lshortfile).SetOptions(
"level", FINEST,
"format", "[%L] [%P] (%S) %M%F")
elog := logger.With("source", "testing")
elog.Info("redirected.")
want := "[INFO] [redirected] (stdlog_test.go) redirected. source=testing\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
buf.Reset()
log := NewStdLog(elog.AddCallerSkip(3))
log.Println("This is stdlog's println.")
want = "[INFO] [redirected] (stdlog_test.go) This is stdlog's println. source=testing\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
buf.Reset()
levels := []int{FINEST, DEBUG, TRACE, INFO, WARN, ERROR}
for _, level := range levels {
log := NewStdLogAt(elog.AddCallerSkip(3), level)
log.Println("redirected.")
want := "[" + Level(level).String() + "] [redirected] (stdlog_test.go) redirected. source=testing\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
buf.Reset()
}
}
func TestRedirectStdLog(t *testing.T) {
buf := new(bytes.Buffer)
log.SetFlags(log.Lshortfile)
log.SetOutput(buf)
testStdlog := func() {
log.Println("redirected")
want := "stdlog_test.go:54: redirected\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
buf.Reset()
}
testStdlog()
logger := New(buf, "redirected", Lshortfile).SetOptions(
"level", FINEST,
"format", "[%L] [%P] (%S) %M%F")
elog := logger.With("source", "testing")
restoreFunc := RedirectStdLogAt(elog.AddCallerSkip(3), "debug")
log.Println("redirected.")
want := "[DEBG] [redirected] (stdlog_test.go) redirected. source=testing\n"
if got := buf.String(); got != want {
t.Errorf(" got %q", got)
t.Errorf(" want %q", want)
}
buf.Reset()
restoreFunc()
testStdlog()
}