4
4
package log
5
5
6
6
import (
7
+ "regexp"
7
8
"sync"
8
9
"testing"
9
10
"time"
@@ -34,11 +35,11 @@ func (d *dummyWriter) Close() error {
34
35
return nil
35
36
}
36
37
37
- func (d * dummyWriter ) GetLogs () []string {
38
+ func (d * dummyWriter ) FetchLogs () []string {
38
39
d .mu .Lock ()
39
40
defer d .mu .Unlock ()
40
- logs := make ([] string , len ( d .logs ))
41
- copy ( logs , d .logs )
41
+ logs := d .logs
42
+ d .logs = nil
42
43
return logs
43
44
}
44
45
@@ -76,14 +77,14 @@ func TestLogger(t *testing.T) {
76
77
77
78
// w2 is slow, so only w1 has logs
78
79
time .Sleep (100 * time .Millisecond )
79
- assert .Equal (t , []string {"debug-level\n " , "error-level\n " }, w1 .GetLogs ())
80
- assert .Equal (t , [] string {}, w2 .GetLogs ())
80
+ assert .Equal (t , []string {"debug-level\n " , "error-level\n " }, w1 .FetchLogs ())
81
+ assert .Empty (t , w2 .FetchLogs ())
81
82
82
83
logger .Close ()
83
84
84
85
// after Close, all logs are flushed
85
- assert .Equal (t , [] string { "debug-level \n " , "error-level \n " }, w1 .GetLogs ())
86
- assert .Equal (t , []string {"error-level\n " }, w2 .GetLogs ())
86
+ assert .Empty (t , w1 .FetchLogs ())
87
+ assert .Equal (t , []string {"error-level\n " }, w2 .FetchLogs ())
87
88
}
88
89
89
90
func TestLoggerPause (t * testing.T ) {
@@ -97,12 +98,12 @@ func TestLoggerPause(t *testing.T) {
97
98
98
99
logger .Info ("info-level" )
99
100
time .Sleep (100 * time .Millisecond )
100
- assert .Equal (t , [] string {}, w1 .GetLogs ())
101
+ assert .Empty (t , w1 .FetchLogs ())
101
102
102
103
GetManager ().ResumeAll ()
103
104
104
105
time .Sleep (100 * time .Millisecond )
105
- assert .Equal (t , []string {"info-level\n " }, w1 .GetLogs ())
106
+ assert .Equal (t , []string {"info-level\n " }, w1 .FetchLogs ())
106
107
107
108
logger .Close ()
108
109
}
@@ -123,21 +124,42 @@ func (t *testLogStringPtrReceiver) LogString() string {
123
124
return "log-string-ptr-receiver"
124
125
}
125
126
126
- func TestLoggerLogString (t * testing.T ) {
127
- logger := NewLoggerWithWriters (t .Context (), "test" )
128
-
129
- w1 := newDummyWriter ("dummy-1" , DEBUG , 0 )
130
- w1 .Mode .Colorize = true
131
- logger .AddWriters (w1 )
132
-
133
- logger .Info ("%s %s %#v %v" , testLogString {}, & testLogString {}, testLogString {Field : "detail" }, NewColoredValue (testLogString {}, FgRed ))
134
- logger .Info ("%s %s %#v %v" , testLogStringPtrReceiver {}, & testLogStringPtrReceiver {}, testLogStringPtrReceiver {Field : "detail" }, NewColoredValue (testLogStringPtrReceiver {}, FgRed ))
135
- logger .Close ()
127
+ func genericFunc [T any ](logger Logger , v T ) {
128
+ logger .Info ("from genericFunc: %v" , v )
129
+ }
136
130
137
- assert .Equal (t , []string {
138
- "log-string log-string log.testLogString{Field:\" detail\" } \x1b [31mlog-string\x1b [0m\n " ,
139
- "log-string-ptr-receiver log-string-ptr-receiver &log.testLogStringPtrReceiver{Field:\" detail\" } \x1b [31mlog-string-ptr-receiver\x1b [0m\n " ,
140
- }, w1 .GetLogs ())
131
+ func TestLoggerOutput (t * testing.T ) {
132
+ t .Run ("LogString" , func (t * testing.T ) {
133
+ logger := NewLoggerWithWriters (t .Context (), "test" )
134
+ w1 := newDummyWriter ("dummy-1" , DEBUG , 0 )
135
+ w1 .Mode .Colorize = true
136
+ logger .AddWriters (w1 )
137
+ logger .Info ("%s %s %#v %v" , testLogString {}, & testLogString {}, testLogString {Field : "detail" }, NewColoredValue (testLogString {}, FgRed ))
138
+ logger .Info ("%s %s %#v %v" , testLogStringPtrReceiver {}, & testLogStringPtrReceiver {}, testLogStringPtrReceiver {Field : "detail" }, NewColoredValue (testLogStringPtrReceiver {}, FgRed ))
139
+ logger .Close ()
140
+
141
+ assert .Equal (t , []string {
142
+ "log-string log-string log.testLogString{Field:\" detail\" } \x1b [31mlog-string\x1b [0m\n " ,
143
+ "log-string-ptr-receiver log-string-ptr-receiver &log.testLogStringPtrReceiver{Field:\" detail\" } \x1b [31mlog-string-ptr-receiver\x1b [0m\n " ,
144
+ }, w1 .FetchLogs ())
145
+ })
146
+
147
+ t .Run ("Caller" , func (t * testing.T ) {
148
+ logger := NewLoggerWithWriters (t .Context (), "test" )
149
+ w1 := newDummyWriter ("dummy-1" , DEBUG , 0 )
150
+ w1 .EventWriterBaseImpl .Mode .Flags .flags = Lmedfile | Lshortfuncname
151
+ logger .AddWriters (w1 )
152
+ anonymousFunc := func (logger Logger ) {
153
+ logger .Info ("from anonymousFunc" )
154
+ }
155
+ genericFunc (logger , "123" )
156
+ anonymousFunc (logger )
157
+ logger .Close ()
158
+ logs := w1 .FetchLogs ()
159
+ assert .Len (t , logs , 2 )
160
+ assert .Regexp (t , `modules/log/logger_test.go:\w+:` + regexp .QuoteMeta (`genericFunc() from genericFunc: 123` ), logs [0 ])
161
+ assert .Regexp (t , `modules/log/logger_test.go:\w+:` + regexp .QuoteMeta (`TestLoggerOutput.2.1() from anonymousFunc` ), logs [1 ])
162
+ })
141
163
}
142
164
143
165
func TestLoggerExpressionFilter (t * testing.T ) {
@@ -153,5 +175,5 @@ func TestLoggerExpressionFilter(t *testing.T) {
153
175
logger .SendLogEvent (& Event {Level : INFO , Filename : "foo.go" , MsgSimpleText : "by filename" })
154
176
logger .Close ()
155
177
156
- assert .Equal (t , []string {"foo\n " , "foo bar\n " , "by filename\n " }, w1 .GetLogs ())
178
+ assert .Equal (t , []string {"foo\n " , "foo bar\n " , "by filename\n " }, w1 .FetchLogs ())
157
179
}
0 commit comments