@@ -348,68 +348,69 @@ func Panicj(j JSON) {
348
348
}
349
349
350
350
func (l * Logger ) log (v Lvl , format string , args ... interface {}) {
351
+ if ! (v >= l .level || v == 0 ) {
352
+ return
353
+ }
351
354
l .mutex .Lock ()
352
355
defer l .mutex .Unlock ()
353
356
buf := l .bufferPool .Get ().(* bytes.Buffer )
354
357
buf .Reset ()
355
358
defer l .bufferPool .Put (buf )
356
359
_ , file , line , _ := runtime .Caller (l .skip )
357
360
358
- if v >= l .level || v == 0 {
359
- message := ""
360
- if format == "" {
361
- message = fmt .Sprint (args ... )
362
- } else if format == "json" {
363
- b , err := json .Marshal (args [0 ])
364
- if err != nil {
365
- panic (err )
366
- }
367
- message = string (b )
368
- } else {
369
- message = fmt .Sprintf (format , args ... )
361
+ message := ""
362
+ if format == "" {
363
+ message = fmt .Sprint (args ... )
364
+ } else if format == "json" {
365
+ b , err := json .Marshal (args [0 ])
366
+ if err != nil {
367
+ panic (err )
370
368
}
369
+ message = string (b )
370
+ } else {
371
+ message = fmt .Sprintf (format , args ... )
372
+ }
371
373
372
- _ , err := l .template .ExecuteFunc (buf , func (w io.Writer , tag string ) (int , error ) {
373
- switch tag {
374
- case "time_rfc3339" :
375
- return w .Write ([]byte (time .Now ().Format (time .RFC3339 )))
376
- case "time_rfc3339_nano" :
377
- return w .Write ([]byte (time .Now ().Format (time .RFC3339Nano )))
378
- case "level" :
379
- return w .Write ([]byte (l .levels [v ]))
380
- case "prefix" :
381
- return w .Write ([]byte (l .prefix ))
382
- case "long_file" :
383
- return w .Write ([]byte (file ))
384
- case "short_file" :
385
- return w .Write ([]byte (path .Base (file )))
386
- case "line" :
387
- return w .Write ([]byte (strconv .Itoa (line )))
388
- }
389
- return 0 , nil
390
- })
391
-
392
- if err == nil {
393
- s := buf .String ()
394
- i := buf .Len () - 1
395
- if s [i ] == '}' {
396
- // JSON header
397
- buf .Truncate (i )
398
- buf .WriteByte (',' )
399
- if format == "json" {
400
- buf .WriteString (message [1 :])
401
- } else {
402
- buf .WriteString (`"message":` )
403
- buf .WriteString (strconv .Quote (message ))
404
- buf .WriteString (`}` )
405
- }
374
+ _ , err := l .template .ExecuteFunc (buf , func (w io.Writer , tag string ) (int , error ) {
375
+ switch tag {
376
+ case "time_rfc3339" :
377
+ return w .Write ([]byte (time .Now ().Format (time .RFC3339 )))
378
+ case "time_rfc3339_nano" :
379
+ return w .Write ([]byte (time .Now ().Format (time .RFC3339Nano )))
380
+ case "level" :
381
+ return w .Write ([]byte (l .levels [v ]))
382
+ case "prefix" :
383
+ return w .Write ([]byte (l .prefix ))
384
+ case "long_file" :
385
+ return w .Write ([]byte (file ))
386
+ case "short_file" :
387
+ return w .Write ([]byte (path .Base (file )))
388
+ case "line" :
389
+ return w .Write ([]byte (strconv .Itoa (line )))
390
+ }
391
+ return 0 , nil
392
+ })
393
+
394
+ if err == nil {
395
+ s := buf .String ()
396
+ i := buf .Len () - 1
397
+ if s [i ] == '}' {
398
+ // JSON header
399
+ buf .Truncate (i )
400
+ buf .WriteByte (',' )
401
+ if format == "json" {
402
+ buf .WriteString (message [1 :])
406
403
} else {
407
- // Text header
408
- buf .WriteByte ( ' ' )
409
- buf .WriteString (message )
404
+ buf . WriteString ( `"message":` )
405
+ buf .WriteString ( strconv . Quote ( message ) )
406
+ buf .WriteString (`}` )
410
407
}
411
- buf .WriteByte ('\n' )
412
- l .output .Write (buf .Bytes ())
408
+ } else {
409
+ // Text header
410
+ buf .WriteByte (' ' )
411
+ buf .WriteString (message )
413
412
}
413
+ buf .WriteByte ('\n' )
414
+ l .output .Write (buf .Bytes ())
414
415
}
415
416
}
0 commit comments