-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging_default.go
71 lines (60 loc) · 1.81 KB
/
logging_default.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 chalk
import (
"fmt"
"io"
"os"
)
// StdOutLeveledLogger is a leveled logger implementation.
//
// It prints warnings and errors to `os.Stderr` and other messages to
// `os.Stdout`.
type StdOutLeveledLogger 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 *StdOutLeveledLogger) 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 *StdOutLeveledLogger) 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 *StdOutLeveledLogger) 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 *StdOutLeveledLogger) Warnf(format string, v ...interface{}) {
if l.Level >= LevelWarn {
fmt.Fprintf(l.stderr(), "[WARN] "+format+"\n", v...)
}
}
func (l *StdOutLeveledLogger) stderr() io.Writer {
if l.stderrOverride != nil {
return l.stderrOverride
}
return os.Stderr
}
func (l *StdOutLeveledLogger) stdout() io.Writer {
if l.stdoutOverride != nil {
return l.stdoutOverride
}
return os.Stdout
}