-
Notifications
You must be signed in to change notification settings - Fork 0
/
stdouthandler.go
executable file
·102 lines (89 loc) · 2.41 KB
/
stdouthandler.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
package verbose
import (
"fmt"
"io"
"os"
)
// Color is an escaped color code for the terminal
type Color string
// Pre-defined colors
const (
ColorReset Color = "\033[0m"
ColorRed Color = "\033[31m"
ColorGreen Color = "\033[32m"
ColorYellow Color = "\033[33m"
ColorBlue Color = "\033[34m"
ColorMagenta Color = "\033[35m"
ColorCyan Color = "\033[36m"
ColorWhite Color = "\033[37m"
ColorGrey Color = "\033[90m"
)
var colors = map[LogLevel]Color{
LogLevelDebug: ColorBlue,
LogLevelInfo: ColorCyan,
LogLevelNotice: ColorCyan,
LogLevelWarning: ColorMagenta,
LogLevelError: ColorRed,
LogLevelCritical: ColorRed,
LogLevelAlert: ColorRed,
LogLevelEmergency: ColorRed,
LogLevelFatal: ColorRed,
}
// StdoutHandler writes log message to standard out
// It even uses color!
type StdoutHandler struct {
min LogLevel
max LogLevel
out io.Writer // Usually os.Stdout, mainly used for testing
formatter Formatter
}
// NewStdoutHandler creates a new StdoutHandler, surprise!
// Color specifies if the log messages will be printed to a colored terminal.
func NewStdoutHandler(color bool) *StdoutHandler {
var formatter Formatter
if color {
formatter = NewColoredLineFormatter()
} else {
formatter = NewLineFormatter()
}
return &StdoutHandler{
min: LogLevelDebug,
max: LogLevelFatal,
out: os.Stdout,
formatter: formatter,
}
}
// SetLevel will set both the minimum and maximum log levels to l. This makes
// the handler only respond to the single level l.
func (s *StdoutHandler) SetLevel(l LogLevel) {
s.min = l
s.max = l
}
// SetMinLevel will set the minimum log level the handler will handle.
func (s *StdoutHandler) SetMinLevel(l LogLevel) {
if l > s.max {
return
}
s.min = l
}
// SetMaxLevel will set the maximum log level the handler will handle.
func (s *StdoutHandler) SetMaxLevel(l LogLevel) {
if l < s.min {
return
}
s.max = l
}
// SetFormatter gives StdoutHandler a formatter for log messages.
func (s *StdoutHandler) SetFormatter(f Formatter) {
s.formatter = f
}
// Handles returns whether the handler handles log level l.
func (s *StdoutHandler) Handles(l LogLevel) bool {
return (s.min <= l && l <= s.max)
}
// WriteLog writes the log message to standard output
func (s *StdoutHandler) WriteLog(e *Entry) {
fmt.Fprint(s.out, s.formatter.Format(e))
}
// Close satisfies the interface, NOOP
func (s *StdoutHandler) Close() {}