@@ -7,6 +7,7 @@ package logx_test
77
88import (
99 "bytes"
10+ "context"
1011 "fmt"
1112 "io"
1213 "sync"
@@ -23,6 +24,11 @@ type mockWriter struct {
2324 l sync.Mutex
2425}
2526
27+ type testData struct {
28+ A string
29+ B int
30+ }
31+
2632func newMockWriter () * mockWriter {
2733 return & mockWriter {
2834 b : bytes .NewBuffer (nil ),
@@ -45,16 +51,16 @@ func (v *mockWriter) String() string {
4551func TestUnit_NewJSON (t * testing.T ) {
4652 casecheck .NotNil (t , logx .Default ())
4753
48- wg := syncing .NewGroup ()
54+ wg := syncing .NewGroup (context . TODO () )
4955 buff := newMockWriter ()
5056
5157 logx .SetOutput (buff )
5258 logx .SetLevel (logx .LevelDebug )
5359
54- wg .Background (func () { logx .Info ("async" , "id" , 1 ) })
55- wg .Background (func () { logx .Warn ("async" , "id" , 2 ) })
56- wg .Background (func () { logx .Error ("async" , "id" , 3 ) })
57- wg .Background (func () { logx .Debug ("async" , "id" , 4 ) })
60+ wg .Background ("" , func (_ context. Context ) { logx .Info ("async" , "id" , 1 ) })
61+ wg .Background ("" , func (_ context. Context ) { logx .Warn ("async" , "id" , 2 ) })
62+ wg .Background ("" , func (_ context. Context ) { logx .Error ("async" , "id" , 3 ) })
63+ wg .Background ("" , func (_ context. Context ) { logx .Debug ("async" , "id" , 4 ) })
5864
5965 logx .Info ("sync" , "id" , 1 )
6066 logx .Warn ("sync" , "id" , 2 )
@@ -65,6 +71,7 @@ func TestUnit_NewJSON(t *testing.T) {
6571 logx .Info ("context2" , "nil" , nil )
6672 logx .Info ("context3" , "func" , func () {})
6773 logx .Info ("context4" , "err" , fmt .Errorf ("er1" ))
74+ logx .Info ("context5" , "obj" , testData {})
6875
6976 wg .Wait ()
7077
@@ -81,10 +88,11 @@ func TestUnit_NewJSON(t *testing.T) {
8188 casecheck .Contains (t , data , `"level":"INFO","msg":"context2","ctx":{"nil":"null"}` )
8289 casecheck .Contains (t , data , `"level":"INFO","msg":"context3","ctx":{"func":"(func())(0x` )
8390 casecheck .Contains (t , data , `"level":"INFO","msg":"context4","ctx":{"err":"er1"}` )
91+ casecheck .Contains (t , data , `"level":"INFO","msg":"context5","ctx":{"obj":"logx_test.testData{A:\"\", B:0}"}` )
8492}
8593
8694func TestUnit_NewString (t * testing.T ) {
87- wg := syncing .NewGroup ()
95+ wg := syncing .NewGroup (context . TODO () )
8896 buff := newMockWriter ()
8997
9098 l := logx .New ()
@@ -94,10 +102,10 @@ func TestUnit_NewString(t *testing.T) {
94102 l .SetLevel (logx .LevelDebug )
95103 casecheck .Equal (t , logx .LevelDebug , l .GetLevel ())
96104
97- wg .Background (func () { l .Info ("async" , "id" , 1 ) })
98- wg .Background (func () { l .Warn ("async" , "id" , 2 ) })
99- wg .Background (func () { l .Error ("async" , "id" , 3 ) })
100- wg .Background (func () { l .Debug ("async" , "id" , 4 ) })
105+ wg .Background ("" , func (_ context. Context ) { l .Info ("async" , "id" , 1 ) })
106+ wg .Background ("" , func (_ context. Context ) { l .Warn ("async" , "id" , 2 ) })
107+ wg .Background ("" , func (_ context. Context ) { l .Error ("async" , "id" , 3 ) })
108+ wg .Background ("" , func (_ context. Context ) { l .Debug ("async" , "id" , 4 ) })
101109
102110 l .Info ("sync" , "id" , 1 )
103111 l .Warn ("sync" , "id" , 2 )
@@ -108,6 +116,7 @@ func TestUnit_NewString(t *testing.T) {
108116 l .Info ("context2" , "nil" , nil )
109117 l .Info ("context3" , "func" , func () {})
110118 l .Info ("context4" , "err" , fmt .Errorf ("er1" ))
119+ l .Info ("context5" , "obj" , testData {})
111120
112121 wg .Wait ()
113122
@@ -124,10 +133,11 @@ func TestUnit_NewString(t *testing.T) {
124133 casecheck .Contains (t , data , "\" level\" =\" INFO\" \t \" msg\" =\" context2\" \t \" nil\" =\" null\" " )
125134 casecheck .Contains (t , data , "\" level\" =\" INFO\" \t \" msg\" =\" context3\" \t \" func\" =\" (func())(0x" )
126135 casecheck .Contains (t , data , "\" level\" =\" INFO\" \t \" msg\" =\" context4\" \t \" err\" =\" er1\" " )
136+ casecheck .Contains (t , data , "\" level\" =\" INFO\" \t \" msg\" =\" context5\" \t \" obj\" =\" logx_test.testData{A:\" \" , B:0}\" " )
127137}
128138
129139func TestUnit_NewSlog (t * testing.T ) {
130- wg := syncing .NewGroup ()
140+ wg := syncing .NewGroup (context . TODO () )
131141 buff := newMockWriter ()
132142
133143 l := logx .NewSLogJsonAdapter ()
@@ -136,10 +146,10 @@ func TestUnit_NewSlog(t *testing.T) {
136146 l .SetOutput (buff )
137147 l .SetLevel (logx .LevelDebug )
138148
139- wg .Background (func () { l .Info ("async" , "id" , 1 ) })
140- wg .Background (func () { l .Warn ("async" , "id" , 2 ) })
141- wg .Background (func () { l .Error ("async" , "id" , 3 ) })
142- wg .Background (func () { l .Debug ("async" , "id" , 4 ) })
149+ wg .Background ("" , func (_ context. Context ) { l .Info ("async" , "id" , 1 ) })
150+ wg .Background ("" , func (_ context. Context ) { l .Warn ("async" , "id" , 2 ) })
151+ wg .Background ("" , func (_ context. Context ) { l .Error ("async" , "id" , 3 ) })
152+ wg .Background ("" , func (_ context. Context ) { l .Debug ("async" , "id" , 4 ) })
143153
144154 l .Info ("sync" , "id" , 1 )
145155 l .Warn ("sync" , "id" , 2 )
@@ -150,6 +160,7 @@ func TestUnit_NewSlog(t *testing.T) {
150160 l .Info ("context2" , "nil" , nil )
151161 l .Info ("context3" , "func" , func () {})
152162 l .Info ("context4" , "err" , fmt .Errorf ("er1" ))
163+ l .Info ("context5" , "obj" , testData {})
153164
154165 wg .Wait ()
155166
@@ -166,8 +177,24 @@ func TestUnit_NewSlog(t *testing.T) {
166177 casecheck .Contains (t , data , "\" level\" :\" INFO\" ,\" msg\" :\" context2\" ,\" nil\" :null" )
167178 casecheck .Contains (t , data , "\" level\" :\" INFO\" ,\" msg\" :\" context3\" ,\" func\" :\" !ERROR:json: unsupported type: func()\" " )
168179 casecheck .Contains (t , data , "\" level\" :\" INFO\" ,\" msg\" :\" context4\" ,\" err\" :\" er1\" " )
180+ casecheck .Contains (t , data , "\" level\" :\" INFO\" ,\" msg\" :\" context5\" ,\" obj\" :{\" A\" :\" \" ,\" B\" :0}" )
169181}
170182
183+ /*
184+ goos: linux
185+ goarch: amd64
186+ pkg: go.osspkg.com/logx
187+ cpu: 12th Gen Intel(R) Core(TM) i9-12900KF
188+ BenchmarkNewJSON
189+ BenchmarkNewJSON-24 152106 9702 ns/op 66942 B/op 13 allocs/op
190+ BenchmarkNewString
191+ BenchmarkNewString-24 130413 9213 ns/op 66085 B/op 15 allocs/op
192+ BenchmarkNewSLogJsonAdapter
193+ BenchmarkNewSLogJsonAdapter-24 3046932 484.7 ns/op 201 B/op 4 allocs/op
194+ BenchmarkNewSLogStringAdapter
195+ BenchmarkNewSLogStringAdapter-24 4683210 308.7 ns/op 96 B/op 3 allocs/op
196+ */
197+
171198func BenchmarkNewJSON (b * testing.B ) {
172199 ll := logx .New ()
173200 ll .SetOutput (io .Discard )
@@ -178,7 +205,7 @@ func BenchmarkNewJSON(b *testing.B) {
178205 b .ResetTimer ()
179206 b .RunParallel (func (pb * testing.PB ) {
180207 for pb .Next () {
181- ll .Info ("sync" , "id" , 1 )
208+ ll .Info ("sync" , "id" , 1 , "obj" , testData {} )
182209 }
183210 })
184211}
@@ -193,7 +220,7 @@ func BenchmarkNewString(b *testing.B) {
193220 b .ResetTimer ()
194221 b .RunParallel (func (pb * testing.PB ) {
195222 for pb .Next () {
196- ll .Info ("sync" , "id" , 1 )
223+ ll .Info ("sync" , "id" , 1 , "obj" , testData {} )
197224 }
198225 })
199226}
@@ -207,7 +234,7 @@ func BenchmarkNewSLogJsonAdapter(b *testing.B) {
207234 b .ResetTimer ()
208235 b .RunParallel (func (pb * testing.PB ) {
209236 for pb .Next () {
210- ll .Info ("sync" , "id" , 1 )
237+ ll .Info ("sync" , "id" , 1 , "obj" , testData {} )
211238 }
212239 })
213240}
@@ -221,7 +248,7 @@ func BenchmarkNewSLogStringAdapter(b *testing.B) {
221248 b .ResetTimer ()
222249 b .RunParallel (func (pb * testing.PB ) {
223250 for pb .Next () {
224- ll .Info ("sync" , "id" , 1 )
251+ ll .Info ("sync" , "id" , 1 , "obj" , testData {} )
225252 }
226253 })
227254}
0 commit comments