/
console.go
69 lines (55 loc) · 1.56 KB
/
console.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
package handler
import (
"os"
"sync"
"github.com/thinkoner/thinkgo/log/record"
)
type brush func(string) string
func newBrush(color string) brush {
pre := "\033["
reset := "\033[0m"
return func(text string) string {
return pre + color + "m" + text + reset
}
}
var colors = map[record.Level]brush{
record.EMERGENCY: newBrush("1;41"), // Emergency Red background
record.ALERT: newBrush("1;35"), // Alert purple
record.CRITICAL: newBrush("1;34"), // Critical blue
record.ERROR: newBrush("1;31"), // Error red
record.WARNING: newBrush("1;33"), // Warn yellow
record.INFO: newBrush("1;36"), // Informational sky blue
record.DEBUG: newBrush("1;32"), // Debug green
record.NOTICE: newBrush("1;32"), // Trace green
}
type ConsoleHandler struct {
Handler
sync.Mutex
level record.Level
bubble bool
}
func NewConsoleHandler(level record.Level) *ConsoleHandler {
return &ConsoleHandler{
level: level,
bubble: true,
}
}
// IsHandling Checks whether the given record will be handled by this handler.
func (h *ConsoleHandler) IsHandling(r record.Record) bool {
return r.Level >= h.level
}
// Handle Handles a record.
func (h *ConsoleHandler) Handle(r record.Record) bool {
if !h.IsHandling(r) {
return false
}
r.Formatted = h.GetFormatter().Format(r)
h.write(r)
return false == h.bubble
}
func (h *ConsoleHandler) write(r record.Record) {
h.Lock()
defer h.Unlock()
message := colors[r.Level](r.Formatted)
os.Stdout.Write(append([]byte(message)))
}