github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/zlog/trace_test.go (about) 1 package zlog 2 3 import ( 4 "context" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "go.uber.org/zap" 10 "go.uber.org/zap/zaptest" 11 ) 12 13 func TestTrace(t *testing.T) { 14 buf := &zaptest.Buffer{} 15 l, p, _ := NewWithOutput(&Config{Level: "trace", Format: "logfmt"}, buf) 16 defer ReplaceGlobals(l, p)() 17 18 L().Trace("trace message", zap.String("k1", "v1")) 19 20 got := buf.String() 21 assert.Contains(t, got, "level=trace") 22 assert.Contains(t, got, "k1=v1") 23 assert.Contains(t, got, "caller=zlog/trace_test.go:18") 24 assert.Contains(t, got, "level=trace") 25 assert.Contains(t, got, `msg="trace message"`) 26 } 27 28 func TestTracef(t *testing.T) { 29 buf := &zaptest.Buffer{} 30 l, p, _ := NewWithOutput(&Config{Level: "trace", Format: "logfmt"}, buf) 31 defer ReplaceGlobals(l, p)() 32 33 S().Tracef("trace message, %v, %v", 123, 456) 34 35 got := buf.String() 36 assert.Contains(t, got, "level=trace") 37 assert.Regexp(t, `caller=zlog/trace_test\.go:\d+`, got) 38 assert.Contains(t, got, "level=trace") 39 assert.Contains(t, got, `msg="trace message, 123, 456"`) 40 } 41 42 func TestTRACE(t *testing.T) { 43 buf := &zaptest.Buffer{} 44 l, p, _ := NewWithOutput(&Config{Level: "trace", Development: true}, buf) 45 defer ReplaceGlobals(l, p)() 46 47 TRACE() 48 got := buf.String() 49 assert.Regexp(t, `zlog/trace_test\.go:\d+`, got) 50 51 buf.Reset() 52 TRACE(WithLogger(context.Background(), L().With(zap.Int("TestTRACE", 1)))) 53 TRACE(L().With(zap.Int("TestTRACE", 1))) 54 TRACE(S().With("TestTRACE", 1)) 55 for _, line := range buf.Lines() { 56 assert.Contains(t, line, " TRACE ") 57 assert.Regexp(t, `zlog/trace_test.go:5\d`, line) 58 assert.Contains(t, line, `"TestTRACE": 1`) 59 } 60 61 buf.Reset() 62 TRACE("a", "b", "c", 1, 2, 3) 63 TRACE(context.Background(), "a", "b", "c", 1, 2, 3) 64 TRACE(L(), "a", "b", "c", 1, 2, 3) 65 TRACE(S(), "a", "b", "c", 1, 2, 3) 66 for _, line := range buf.Lines() { 67 assert.Contains(t, line, " TRACE ") 68 assert.Regexp(t, `zlog/trace_test\.go:6\d`, line) 69 assert.Contains(t, line, "abc1 2 3") 70 } 71 72 buf.Reset() 73 TRACE("a=%v, b=%v, c=%v", 1, 2, 3) 74 TRACE(context.Background(), "a=%v, b=%v, c=%v", 1, 2, 3) 75 TRACE(L(), "a=%v, b=%v, c=%v", 1, 2, 3) 76 TRACE(S(), "a=%v, b=%v, c=%v", 1, 2, 3) 77 for _, line := range buf.Lines() { 78 assert.Contains(t, line, " TRACE ") 79 assert.Regexp(t, `zlog/trace_test\.go:7\d`, line) 80 assert.Contains(t, line, "a=1, b=2, c=3") 81 } 82 } 83 84 func TestTRACESkip(t *testing.T) { 85 buf := &zaptest.Buffer{} 86 l, p, _ := NewWithOutput(&Config{Level: "trace"}, buf) 87 defer ReplaceGlobals(l, p)() 88 89 TRACE() 90 TRACESkip(0) 91 wrappedTRACE() // this line outputs two messages 92 93 lines := buf.Lines() 94 assert.Len(t, lines, 4) 95 for _, line := range lines { 96 assert.Contains(t, line, `"level":"trace"`) 97 assert.Contains(t, line, "========") 98 assert.Contains(t, line, `"level":"trace"`) 99 assert.Contains(t, line, "zlog.TestTRACESkip") 100 assert.Regexp(t, `zlog/trace_test\.go:[89]\d`, line) 101 } 102 } 103 104 func wrappedTRACE(args ...any) { 105 106 var wrappedLevel2 = func(args ...any) { 107 TRACESkip(2, args...) 108 return 109 } 110 111 TRACESkip(1, args...) 112 wrappedLevel2(args...) 113 } 114 115 func wrappedTRACE1(arg0 any, args ...any) { 116 117 var wrappedLevel2 = func(arg0 any, args ...any) { 118 TRACESkip1(2, arg0, args...) 119 return 120 } 121 122 TRACESkip1(1, arg0, args...) 123 wrappedLevel2(arg0, args...) 124 } 125 126 func TestTRACE1(t *testing.T) { 127 buf := &zaptest.Buffer{} 128 l, p, _ := NewWithOutput(&Config{Level: "trace", Development: true}, buf) 129 defer ReplaceGlobals(l, p)() 130 131 TRACE1("a") 132 got := buf.String() 133 assert.Contains(t, got, "zlog/trace_test.go:131") 134 135 buf.Reset() 136 TRACE1(WithLogger(context.Background(), L().With(zap.Int("TestTRACE1", 1)))) 137 TRACE1(L().With(zap.Int("TestTRACE1", 1))) 138 TRACE1(S().With("TestTRACE1", 1)) 139 for _, line := range buf.Lines() { 140 assert.Contains(t, line, " TRACE ") 141 assert.Regexp(t, `zlog/trace_test.go:13\d`, line) 142 assert.Contains(t, line, `"TestTRACE1": 1`) 143 } 144 145 buf.Reset() 146 TRACE1("a", "b", "c", 1, 2, 3) 147 TRACE1(context.Background(), "a", "b", "c", 1, 2, 3) 148 TRACE1(L(), "a", "b", "c", 1, 2, 3) 149 TRACE1(S(), "a", "b", "c", 1, 2, 3) 150 for _, line := range buf.Lines() { 151 assert.Contains(t, line, " TRACE ") 152 assert.Regexp(t, `zlog/trace_test.go:14\d`, line) 153 assert.Contains(t, line, "abc1 2 3") 154 } 155 156 buf.Reset() 157 TRACE1("a=%v, b=%v, c=%v", 1, 2, 3) 158 TRACE1(context.Background(), "a=%v, b=%v, c=%v", 1, 2, 3) 159 TRACE1(L(), "a=%v, b=%v, c=%v", 1, 2, 3) 160 TRACE1(S(), "a=%v, b=%v, c=%v", 1, 2, 3) 161 for _, line := range buf.Lines() { 162 assert.Contains(t, line, " TRACE ") 163 assert.Regexp(t, `zlog/trace_test.go:1[56]\d`, line) 164 assert.Contains(t, line, "a=1, b=2, c=3") 165 } 166 } 167 168 func TestTRACESkip1(t *testing.T) { 169 buf := &zaptest.Buffer{} 170 l, p, _ := NewWithOutput(&Config{Level: "trace"}, buf) 171 defer ReplaceGlobals(l, p)() 172 173 TRACE1("test trace 1") 174 TRACESkip1(0, "test trace 1") 175 wrappedTRACE1("test trace 1") // this line outputs two messages 176 177 lines := buf.Lines() 178 assert.Len(t, lines, 4) 179 for _, line := range lines { 180 t.Log(line) 181 assert.Contains(t, line, `"level":"trace"`) 182 assert.Contains(t, line, "test trace 1") 183 assert.Contains(t, line, `"level":"trace"`) 184 assert.Contains(t, line, "zlog.TestTRACESkip1") 185 assert.Regexp(t, `zlog/trace_test\.go:17\d`, line) 186 } 187 } 188 189 func TestTraceFilterRule(t *testing.T) { 190 msg := "test TraceFilterRule" 191 testCases := []struct { 192 name string 193 rule string 194 contains bool 195 }{ 196 {name: "default", rule: "", contains: true}, 197 {name: "allow all", rule: "allow=all", contains: true}, 198 {name: "allow explicitly 1", rule: "allow=zlog/*.go", contains: true}, 199 {name: "allow explicitly 2", rule: "allow=gopkg/**", contains: true}, 200 {name: "not allowed explicitly", rule: "allow=confr/*,easy/*.go,easy/ezhttp/**", contains: false}, 201 {name: "deny all", rule: "deny=all", contains: false}, 202 {name: "deny explicitly", rule: "deny=zlog/*", contains: false}, 203 {name: "not denied explicitly", rule: "deny=confr/*,easy/ezhttp/**", contains: true}, 204 } 205 for _, tc := range testCases { 206 t.Run(tc.name, func(t *testing.T) { 207 buf := &zaptest.Buffer{} 208 cfg := &Config{Level: "trace", Format: "logfmt"} 209 cfg.TraceFilterRule = tc.rule 210 l, p, _ := NewWithOutput(cfg, buf) 211 defer ReplaceGlobals(l, p)() 212 213 L().Trace(msg) 214 S().Tracef(msg) 215 TRACE(msg) 216 got := buf.String() 217 if tc.contains { 218 assert.Contains(t, got, msg) 219 assert.Equal(t, 3, strings.Count(got, msg)) 220 } else { 221 assert.NotContains(t, got, msg) 222 } 223 }) 224 } 225 }