8
8
"os"
9
9
"runtime"
10
10
"strings"
11
+ "sync"
11
12
"time"
12
13
)
13
14
@@ -25,12 +26,14 @@ const (
25
26
// RecordLogFileName represents the default file name of the record log.
26
27
RecordLogFileName = "sentinel-record.log"
27
28
DefaultDirName = "logs" + string (os .PathSeparator ) + "csp" + string (os .PathSeparator )
29
+ GlobalCallerDepth = 4
28
30
)
29
31
30
32
var (
31
- globalLogLevel = InfoLevel
32
- globalCallerDepth = 4
33
- globalLogger = NewConsoleLogger ()
33
+ globalLogLevel = InfoLevel
34
+ globalLogger = NewConsoleLogger ()
35
+
36
+ FrequentErrorOnce = & sync.Once {}
34
37
)
35
38
36
39
func GetGlobalLoggerLevel () Level {
@@ -169,12 +172,19 @@ func AssembleMsg(depth int, logLevel, msg string, err error, keysAndValues ...in
169
172
sb .WriteString (kStr )
170
173
sb .WriteByte ('"' )
171
174
sb .WriteByte (':' )
172
- if vbs , err := json .Marshal (v ); err != nil {
175
+ vStr , vIsStr := v .(string )
176
+ if ! vIsStr {
177
+ if vbs , err := json .Marshal (v ); err != nil {
178
+ sb .WriteByte ('"' )
179
+ sb .WriteString (fmt .Sprintf ("%+v" , v ))
180
+ sb .WriteByte ('"' )
181
+ } else {
182
+ sb .WriteString (string (vbs ))
183
+ }
184
+ } else {
173
185
sb .WriteByte ('"' )
174
- sb .WriteString (fmt . Sprintf ( "%+v" , v ) )
186
+ sb .WriteString (vStr )
175
187
sb .WriteByte ('"' )
176
- } else {
177
- sb .WriteString (string (vbs ))
178
188
}
179
189
i = i + 2
180
190
}
@@ -191,29 +201,29 @@ func (l *DefaultLogger) Debug(msg string, keysAndValues ...interface{}) {
191
201
if DebugLevel < globalLogLevel {
192
202
return
193
203
}
194
- l .log .Print (AssembleMsg (globalCallerDepth , "DEBUG" , msg , nil , keysAndValues ... ))
204
+ l .log .Print (AssembleMsg (GlobalCallerDepth , "DEBUG" , msg , nil , keysAndValues ... ))
195
205
}
196
206
197
207
func (l * DefaultLogger ) Info (msg string , keysAndValues ... interface {}) {
198
208
if InfoLevel < globalLogLevel {
199
209
return
200
210
}
201
- l .log .Print (AssembleMsg (globalCallerDepth , "INFO" , msg , nil , keysAndValues ... ))
211
+ l .log .Print (AssembleMsg (GlobalCallerDepth , "INFO" , msg , nil , keysAndValues ... ))
202
212
}
203
213
204
214
func (l * DefaultLogger ) Warn (msg string , keysAndValues ... interface {}) {
205
215
if WarnLevel < globalLogLevel {
206
216
return
207
217
}
208
218
209
- l .log .Print (AssembleMsg (globalCallerDepth , "WARNING" , msg , nil , keysAndValues ... ))
219
+ l .log .Print (AssembleMsg (GlobalCallerDepth , "WARNING" , msg , nil , keysAndValues ... ))
210
220
}
211
221
212
222
func (l * DefaultLogger ) Error (err error , msg string , keysAndValues ... interface {}) {
213
223
if ErrorLevel < globalLogLevel {
214
224
return
215
225
}
216
- l .log .Print (AssembleMsg (globalCallerDepth , "ERROR" , msg , err , keysAndValues ... ))
226
+ l .log .Print (AssembleMsg (GlobalCallerDepth , "ERROR" , msg , err , keysAndValues ... ))
217
227
}
218
228
219
229
func Debug (msg string , keysAndValues ... interface {}) {
0 commit comments