-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstdlogger.go
71 lines (61 loc) · 1.55 KB
/
stdlogger.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
package logger
import (
"fmt"
"log"
"os"
)
// StdLogger writes log entries to os.Stderr
type StdLogger struct {
log *log.Logger
logInfo bool
logErr bool
logTrace bool
}
// NewStdLogger returns a new logger that writes to os.Stderr
// using the standard log package.
// By default, it will log info and error entries, but not trace entries.
func NewStdLogger() *StdLogger {
return &StdLogger{
log: log.New(os.Stderr, "", log.LstdFlags),
logErr: true,
logInfo: true,
}
}
// SetFlags sets the output flags for the logger.
func (l *StdLogger) SetFlags(flag int) *StdLogger {
l.log.SetFlags(flag)
return l
}
// SetInfo sets whether info entries should be logged.
func (l *StdLogger) SetInfo(logInfo bool) *StdLogger {
l.logInfo = logInfo
return l
}
// SetErr sets whether error entries should be logged.
func (l *StdLogger) SetErr(logErr bool) *StdLogger {
l.logErr = logErr
return l
}
// SetTrace sets whether trace entries should be logged.
func (l *StdLogger) SetTrace(logTrace bool) *StdLogger {
l.logTrace = logTrace
return l
}
// Infof writes an info log entry.
func (l *StdLogger) Infof(format string, v ...interface{}) {
if l.logInfo {
l.log.Print("[INF] ", fmt.Sprintf(format, v...))
}
}
// Errorf writes an error log entry.
func (l *StdLogger) Errorf(format string, v ...interface{}) {
if l.logErr {
l.log.Print("[ERR] ", fmt.Sprintf(format, v...))
}
}
// Tracef writes a trace log entry.
func (l *StdLogger) Tracef(format string, v ...interface{}) {
if l.logTrace {
l.log.Print("[TRA] ", fmt.Sprintf(format, v...))
}
}