forked from humanlogio/humanlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
83 lines (72 loc) · 1.58 KB
/
handler.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
package humanlog
import (
"time"
"github.com/kr/logfmt"
)
// Handler can recognize it's log lines, parse them and prettify them.
type Handler interface {
CanHandle(line []byte) bool
Prettify(skipUnchanged bool) []byte
logfmt.Handler
}
var DefaultOptions = &HandlerOptions{
SortLongest: true,
SkipUnchanged: true,
Truncates: true,
LightBg: false,
TruncateLength: 15,
KeyRGB: RGB{1, 108, 89},
ValRGB: RGB{125, 125, 125},
TimeFormat: time.Stamp,
}
type RGB struct{ R, G, B uint8 }
func (r *RGB) tuple() (uint8, uint8, uint8) { return r.R, r.G, r.B }
type HandlerOptions struct {
Skip map[string]struct{}
Keep map[string]struct{}
SortLongest bool
SkipUnchanged bool
Truncates bool
LightBg bool
TruncateLength int
KeyRGB RGB
ValRGB RGB
TimeFormat string
}
func (h *HandlerOptions) shouldShowKey(key string) bool {
if len(h.Keep) != 0 {
if _, keep := h.Keep[key]; keep {
return true
}
}
if len(h.Skip) != 0 {
if _, skip := h.Skip[key]; skip {
return false
}
}
return true
}
func (h *HandlerOptions) shouldShowUnchanged(key string) bool {
if len(h.Keep) != 0 {
if _, keep := h.Keep[key]; keep {
return true
}
}
return false
}
func (h *HandlerOptions) SetSkip(skip []string) {
if h.Skip == nil {
h.Skip = make(map[string]struct{})
}
for _, key := range skip {
h.Skip[key] = struct{}{}
}
}
func (h *HandlerOptions) SetKeep(keep []string) {
if h.Keep == nil {
h.Keep = make(map[string]struct{})
}
for _, key := range keep {
h.Keep[key] = struct{}{}
}
}