-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
57 lines (48 loc) · 1.18 KB
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package gcache
import (
"fmt"
"log"
"os"
)
type Logger interface {
Debugf(format string, v ...interface{})
Infof(format string, v ...interface{})
Warnf(format string, v ...interface{})
Errorf(format string, v ...interface{})
Fatalf(format string, v ...interface{})
}
var l Logger = &dl{log.New(os.Stdout, "", log.LstdFlags|log.Lshortfile)}
type dl struct {
*log.Logger
}
const (
callDepth = 3
)
func prefix(lvl, msg string) string {
return fmt.Sprintf("%s: %s", lvl, msg)
}
func (d *dl) Debugf(f string, v ...interface{}) {
d.Output(callDepth, prefix("DEBUG", fmt.Sprintf(f, v...)))
}
func (d *dl) Infof(format string, v ...interface{}) {
d.Output(callDepth, prefix("INFO ", fmt.Sprintf(format, v...)))
}
func (d *dl) Warnf(format string, v ...interface{}) {
d.Output(callDepth, prefix("WARN ", fmt.Sprintf(format, v...)))
}
func (d *dl) Errorf(format string, v ...interface{}) {
d.Output(callDepth, prefix("ERROR", fmt.Sprintf(format, v...)))
}
func (d *dl) Fatalf(format string, v ...interface{}) {
d.Output(callDepth, prefix("FATAL", fmt.Sprintf(format, v...)))
os.Exit(1)
}
func setLogger(logger Logger) {
if logger == nil {
return
}
l = logger
}
func L() Logger {
return l
}