-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_test.go
83 lines (69 loc) · 2.14 KB
/
multi_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
package golog
import (
"bytes"
"runtime"
"testing"
)
// Test that MultiLogger with multiple logger.
func TestMultiLogger(t *testing.T) {
var buf1 bytes.Buffer
var buf2 bytes.Buffer
slice := []Logger{NewStdLogger(&buf1), NewStdLogger(&buf2)}
l := MultiLogger(slice...)
l.Log(LevelInfo, "k1", "v1")
want := `INFO, "k1": "v1"` + "\n"
if got := buf1.String(); got != want {
t.Errorf("buf1.String() = %q want %q", got, want)
}
if got := buf2.String(); got != want {
t.Errorf("buf2.String() = %q want %q", got, want)
}
}
// Test that MultiLogger copies the input slice and is insulated from future modification.
func TestMultiLoggerCopy(t *testing.T) {
var buf bytes.Buffer
slice := []Logger{NewStdLogger(&buf)}
l := MultiLogger(slice...)
slice[0] = nil
l.Log(LevelInfo, "k1", "v1")
if got, want := buf.String(), `INFO, "k1": "v1"`+"\n"; got != want {
t.Errorf("buf.String() = %q want %q", got, want)
}
}
// callDepth returns the logical call depth for the given PCs.
func callDepth(callers []uintptr) (depth int) {
frames := runtime.CallersFrames(callers)
more := true
for more {
_, more = frames.Next()
depth++
}
return
}
// loggerFunc is a Logger implemented by the underlying func.
type loggerFunc func(level Level, kvs ...interface{})
func (f loggerFunc) Log(level Level, kvs ...interface{}) {
f(level, kvs...)
}
// Test that MultiLogger properly flattens chained multiLoggers.
func TestMultiLoggerSingleChainFlatten(t *testing.T) {
pc := make([]uintptr, 1000) // 1000 should fit the full stack
n := runtime.Callers(0, pc)
myDepth := callDepth(pc[:n])
var logDepth int // will contain the depth from which loggerFunc.Logger was called
l := MultiLogger(loggerFunc(func(level Level, kvs ...interface{}) {
n := runtime.Callers(1, pc)
logDepth += callDepth(pc[:n])
}))
ml := l
// chain a bunch of multiLoggers
for i := 0; i < 100; i++ {
ml = MultiLogger(ml)
}
ml = MultiLogger(l, ml, l, ml)
ml.Log(LevelInfo, "k1", "v1")
if logDepth != 4*(myDepth+2) { // 2 should be multiLogger.Log and loggerFunc.Log
t.Errorf("multiLogger did not flatten chained multiLoggers: expected logDepth %d, got %d",
4*(myDepth+2), logDepth)
}
}