-
Notifications
You must be signed in to change notification settings - Fork 349
/
logger.go
61 lines (48 loc) · 1.38 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
58
59
60
61
package req
import (
"io"
"log"
"os"
)
// Logger is the abstract logging interface, gives control to
// the Req users, choice of the logger.
type Logger interface {
Errorf(format string, v ...interface{})
Warnf(format string, v ...interface{})
Debugf(format string, v ...interface{})
}
// NewLogger create a Logger wraps the *log.Logger
func NewLogger(output io.Writer, prefix string, flag int) Logger {
return &logger{l: log.New(output, prefix, flag)}
}
func NewLoggerFromStandardLogger(l *log.Logger) Logger {
return &logger{l: l}
}
func createDefaultLogger() Logger {
return NewLogger(os.Stdout, "", log.Ldate|log.Lmicroseconds)
}
var _ Logger = (*logger)(nil)
type disableLogger struct{}
func (l *disableLogger) Errorf(format string, v ...interface{}) {}
func (l *disableLogger) Warnf(format string, v ...interface{}) {}
func (l *disableLogger) Debugf(format string, v ...interface{}) {}
type logger struct {
l *log.Logger
}
func (l *logger) Errorf(format string, v ...interface{}) {
l.output("ERROR", format, v...)
}
func (l *logger) Warnf(format string, v ...interface{}) {
l.output("WARN", format, v...)
}
func (l *logger) Debugf(format string, v ...interface{}) {
l.output("DEBUG", format, v...)
}
func (l *logger) output(level, format string, v ...interface{}) {
format = level + " [req] " + format
if len(v) == 0 {
l.l.Print(format)
return
}
l.l.Printf(format, v...)
}