forked from op/go-logging
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlog_test.go
More file actions
176 lines (145 loc) · 5.07 KB
/
log_test.go
File metadata and controls
176 lines (145 loc) · 5.07 KB
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
// Copyright 2013, Örjan Persson. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package logging
import (
"bytes"
"io"
"log"
"strings"
"testing"
)
func TestLogCalldepth(t *testing.T) {
buf := &bytes.Buffer{}
SetBackend(NewLogBackend(buf, "", log.Lshortfile))
SetFormatter(MustStringFormatter("%{shortfile} %{level} %{message}"))
log := MustGetLogger("test")
log.Info("test filename")
parts := strings.SplitN(buf.String(), " ", 2)
// Verify that the correct filename is registered by the stdlib logger
if !strings.HasPrefix(parts[0], "log_test.go:") {
t.Errorf("incorrect filename: %s", parts[0])
}
// Verify that the correct filename is registered by go-logging
if !strings.HasPrefix(parts[1], "log_test.go:") {
t.Errorf("incorrect filename: %s", parts[1])
}
}
func c(log *Logger) { log.Info("test callpath") }
func b(log *Logger) { c(log) }
func a(log *Logger) { b(log) }
func rec(log *Logger, r int) {
if r == 0 {
a(log)
return
}
rec(log, r-1)
}
func testCallpath(t *testing.T, format string, mustContain []string, mustStartWith string) {
buf := &bytes.Buffer{}
SetBackend(NewLogBackend(buf, "", log.Lshortfile))
SetFormatter(MustStringFormatter(format))
logger := MustGetLogger("test")
rec(logger, 6)
parts := strings.SplitN(buf.String(), " ", 3)
// Verify that the correct filename is registered by the stdlib logger
if !strings.HasPrefix(parts[0], "log_test.go:") {
t.Errorf("incorrect filename: %s", parts[0])
}
// Verify that the callpath contains expected elements
callpath := parts[1]
if mustStartWith != "" && !strings.HasPrefix(callpath, mustStartWith) {
t.Errorf("incorrect callpath: %s does not start with %s", callpath, mustStartWith)
}
for _, required := range mustContain {
if !strings.Contains(callpath, required) {
t.Errorf("incorrect callpath: %s missing required element %s", callpath, required)
}
}
// Verify that the correct message is registered by go-logging
if !strings.HasPrefix(parts[2], "test callpath") {
t.Errorf("incorrect message: %s", parts[2])
}
}
func TestLogCallpath(t *testing.T) {
// Note: Exact callpath output varies by Go version and architecture due to
// differences in stack trace handling and inlining. We test for essential
// characteristics rather than exact strings.
// Full callpath tests - should contain recursive marker and function names
testCallpath(t, "%{callpath} %{message}", []string{"TestLogCallpath", "rec", "...", "a", "b", "c"}, "TestLogCallpath")
testCallpath(t, "%{callpath:-1} %{message}", []string{"TestLogCallpath", "rec", "...", "a", "b", "c"}, "TestLogCallpath")
testCallpath(t, "%{callpath:0} %{message}", []string{"TestLogCallpath", "rec", "...", "a", "b", "c"}, "TestLogCallpath")
// Depth-limited tests - should start with truncation marker and contain expected functions
testCallpath(t, "%{callpath:1} %{message}", []string{"c"}, "~")
testCallpath(t, "%{callpath:2} %{message}", []string{"c"}, "~")
testCallpath(t, "%{callpath:3} %{message}", []string{"b", "c"}, "~")
}
func BenchmarkLogMemoryBackendIgnored(b *testing.B) {
backend := SetBackend(NewMemoryBackend(1024))
backend.SetLevel(INFO, "")
RunLogBenchmark(b)
}
func BenchmarkLogMemoryBackend(b *testing.B) {
backend := SetBackend(NewMemoryBackend(1024))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
func BenchmarkLogChannelMemoryBackend(b *testing.B) {
channelBackend := NewChannelMemoryBackend(1024)
backend := SetBackend(channelBackend)
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
channelBackend.Flush()
}
func BenchmarkLogLeveled(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(INFO, "")
RunLogBenchmark(b)
}
func BenchmarkLogLogBackend(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
func BenchmarkLogLogBackendColor(b *testing.B) {
colorizer := NewLogBackend(io.Discard, "", 0)
colorizer.Color = true
backend := SetBackend(colorizer)
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
func BenchmarkLogLogBackendStdFlags(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", log.LstdFlags))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
func BenchmarkLogLogBackendLongFileFlag(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", log.Llongfile))
backend.SetLevel(DEBUG, "")
RunLogBenchmark(b)
}
func RunLogBenchmark(b *testing.B) {
password := Password("foo")
log := MustGetLogger("test")
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug("log line for %d and this is rectified: %s", i, password)
}
}
func BenchmarkLogFixed(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(DEBUG, "")
RunLogBenchmarkFixedString(b)
}
func BenchmarkLogFixedIgnored(b *testing.B) {
backend := SetBackend(NewLogBackend(io.Discard, "", 0))
backend.SetLevel(INFO, "")
RunLogBenchmarkFixedString(b)
}
func RunLogBenchmarkFixedString(b *testing.B) {
log := MustGetLogger("test")
b.ResetTimer()
for i := 0; i < b.N; i++ {
log.Debug("some random fixed text")
}
}