-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
128 lines (103 loc) · 3.34 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package mobilepay
import (
"fmt"
"io"
"os"
)
const (
// LevelNull sets a logger to show no messages at all.
LevelNull Level = 0
// LevelError sets a logger to show error messages only.
LevelError Level = 1
// LevelWarn sets a logger to show warning messages or anything more
// severe.
LevelWarn Level = 2
// LevelInfo sets a logger to show informational messages or anything more
// severe.
LevelInfo Level = 3
// LevelDebug sets a logger to show informational messages or anything more
// severe.
LevelDebug Level = 4
)
// DefaultLeveledLogger is the default logger that the library will use to log
// errors, warnings, and informational messages.
var DefaultLeveledLogger LeveledLoggerInterface = &LeveledLogger{
Level: LevelError,
}
var WarnLeveledLogger LeveledLoggerInterface = &LeveledLogger{
Level: LevelWarn,
}
var InfoLeveledLogger LeveledLoggerInterface = &LeveledLogger{
Level: LevelDebug,
}
var DebugLeveledLogger LeveledLoggerInterface = &LeveledLogger{
Level: LevelDebug,
}
// Level represents a logging level.
type Level uint32
// LeveledLogger is a leveled logger implementation.
//
// It prints warnings and errors to `os.Stderr` and other messages to
// `os.Stdout`.
type LeveledLogger struct {
// Level is the minimum logging level that will be emitted by this logger.
//
// For example, a Level set to LevelWarn will emit warnings and errors, but
// not informational or debug messages.
//
// Always set this with a constant like LevelWarn because the individual
// values are not guaranteed to be stable.
Level Level
// Internal testing use only.
stderrOverride io.Writer
stdoutOverride io.Writer
}
// Debugf logs a debug message using Printf conventions.
func (l *LeveledLogger) Debugf(format string, v ...interface{}) {
if l.Level >= LevelDebug {
fmt.Fprintf(l.stdout(), "[DEBUG] "+format+"\n", v...)
}
}
// Errorf logs a warning message using Printf conventions.
func (l *LeveledLogger) Errorf(format string, v ...interface{}) {
// Infof logs a debug message using Printf conventions.
if l.Level >= LevelError {
fmt.Fprintf(l.stderr(), "[ERROR] "+format+"\n", v...)
}
}
// Infof logs an informational message using Printf conventions.
func (l *LeveledLogger) Infof(format string, v ...interface{}) {
if l.Level >= LevelInfo {
fmt.Fprintf(l.stdout(), "[INFO] "+format+"\n", v...)
}
}
// Warnf logs a warning message using Printf conventions.
func (l *LeveledLogger) Warnf(format string, v ...interface{}) {
if l.Level >= LevelWarn {
fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...)
}
}
func (l *LeveledLogger) stderr() io.Writer {
if l.stderrOverride != nil {
return l.stderrOverride
}
return os.Stderr
}
func (l *LeveledLogger) stdout() io.Writer {
if l.stdoutOverride != nil {
return l.stdoutOverride
}
return os.Stdout
}
// LeveledLoggerInterface provides a basic leveled logging interface for
// printing debug, informational, warning, and error messages.
type LeveledLoggerInterface interface {
// Debugf logs a debug message using Printf conventions.
Debugf(format string, v ...interface{})
// Errorf logs a warning message using Printf conventions.
Errorf(format string, v ...interface{})
// Infof logs an informational message using Printf conventions.
Infof(format string, v ...interface{})
// Warnf logs a warning message using Printf conventions.
Warnf(format string, v ...interface{})
}