-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog.go
114 lines (93 loc) · 2.14 KB
/
log.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package adoc
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
)
const (
kCallDepth = 2
)
type Logger struct {
wrapped *log.Logger
debug bool
exitOnFatal bool
}
var logger *Logger
func init() {
logger = &Logger{
wrapped: log.New(os.Stderr, "", log.LstdFlags),
}
}
func UnwrappedLogger() *log.Logger {
return logger.wrapped
}
func EnableDebug() {
logger.debug = true
}
func EnableExitOnFatal() {
logger.exitOnFatal = true
}
func (l *Logger) DebugV(v interface{}) {
l.Debugf("%+v", v)
}
func (l *Logger) Debug(v ...interface{}) {
if l.debug {
l.wrapped.Output(kCallDepth, header("DEBUG", fmt.Sprint(v...)))
}
}
func (l *Logger) Debugf(format string, v ...interface{}) {
if l.debug {
l.wrapped.Output(kCallDepth, header("DEBUG", fmt.Sprintf(format, v...)))
}
}
func (l *Logger) Info(v ...interface{}) {
l.wrapped.Output(kCallDepth, header("INFO", fmt.Sprint(v...)))
}
func (l *Logger) Infof(format string, v ...interface{}) {
l.wrapped.Output(kCallDepth, header("INFO", fmt.Sprintf(format, v...)))
}
func (l *Logger) Warn(v ...interface{}) {
l.wrapped.Output(kCallDepth, header("WARN", fmt.Sprint(v...)))
}
func (l *Logger) Warnf(format string, v ...interface{}) {
l.wrapped.Output(kCallDepth, header("WARN", fmt.Sprintf(format, v...)))
}
func (l *Logger) Error(v ...interface{}) {
l.wrapped.Output(kCallDepth, header("ERROR", fmt.Sprint(v...)))
}
func (l *Logger) Errorf(format string, v ...interface{}) {
l.wrapped.Output(kCallDepth, header("ERROR", fmt.Sprintf(format, v...)))
}
func (l *Logger) Fatal(v ...interface{}) {
msg := header("FATAL", fmt.Sprint(v...))
l.wrapped.Output(kCallDepth, msg)
if l.exitOnFatal {
os.Exit(1)
} else {
panic(msg)
}
}
func (l *Logger) Fatalf(format string, v ...interface{}) {
msg := header("FATAL", fmt.Sprintf(format, v...))
l.wrapped.Output(kCallDepth, msg)
if l.exitOnFatal {
os.Exit(1)
} else {
panic(msg)
}
}
func header(level, msg string) string {
_, file, line, ok := runtime.Caller(kCallDepth)
if ok {
file = filepath.Base(file)
}
if len(file) == 0 {
file = "???"
}
if line < 0 {
line = 0
}
return fmt.Sprintf("%s %s:%d: %s", level, file, line, msg)
}