-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
95 lines (78 loc) · 1.93 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
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
package utils
import (
"io"
"log"
)
// NullLogger stores the default empty logger to be used.
var NullLogger Logger = &nopLogger{}
// LogLevel represents the code for the log severity level.
type LogLevel int
const (
// INFO log severity code.
INFO = iota
// WARN log severity code.
WARN
// ERROR log severity code.
ERROR
)
// Logger defines a simple logging interface
type Logger interface {
Infof(format string, args ...interface{})
Warningf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// FileLogger represents a high-level logger supporting multiple log levels.
type FileLogger struct {
info *log.Logger
warn *log.Logger
error *log.Logger
}
// NewFileLogger creates a new FileLogger that writes in the given io.Writer.
func NewFileLogger(w io.Writer, lvl LogLevel) *FileLogger {
l := &FileLogger{}
flag := log.Ldate | log.Ltime | log.Lmicroseconds
if lvl <= INFO {
l.info = log.New(w, "INFO: ", flag)
}
if lvl <= WARN {
l.warn = log.New(w, "WARN: ", flag)
}
if lvl <= ERROR {
l.error = log.New(w, "ERR: ", flag)
}
return l
}
// Infof writes an info event in the log.
func (f *FileLogger) Infof(format string, args ...interface{}) {
if f.info == nil {
return
}
f.info.Printf(format, args...)
}
// Warningf writes a warning event in the log.
func (f *FileLogger) Warningf(format string, args ...interface{}) {
if f.warn == nil {
return
}
f.warn.Printf(format, args...)
}
// Errorf writes an error event in the log.
func (f *FileLogger) Errorf(format string, args ...interface{}) {
if f.error == nil {
return
}
f.error.Printf(format, args...)
}
type nopLogger struct{}
func (*nopLogger) Infof(format string, args ...interface{}) {
}
func (*nopLogger) Warningf(format string, args ...interface{}) {
}
func (*nopLogger) Errorf(format string, args ...interface{}) {
}
func (*nopLogger) Info(string) {
}
func (*nopLogger) Warning(string) {
}
func (*nopLogger) Error(string) {
}