-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
stdout.go
157 lines (143 loc) · 3.54 KB
/
stdout.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package stdout
import (
"fmt"
"strings"
"github.com/k1LoW/harvest/parser"
"github.com/labstack/gommon/color"
)
const (
tsParseFmt = "2006-01-02T15:04:05-07:00"
tsNanoParseFmt = "2006-01-02T15:04:05.000000000-07:00"
)
var colorizeMap = []struct {
colorFunc func(interface{}, ...string) string
bar string
}{
{color.Yellow, "█ "},
{color.Magenta, "█ "},
{color.Green, "█ "},
{color.Cyan, "█ "},
{color.Yellow, "▚ "},
{color.Magenta, "▚ "},
{color.Green, "▚ "},
{color.Cyan, "▚ "},
{color.Yellow, "║ "},
{color.Magenta, "║ "},
{color.Green, "║ "},
{color.Cyan, "║ "},
{color.Yellow, "▒ "},
{color.Magenta, "▒ "},
{color.Green, "▒ "},
{color.Cyan, "▒ "},
{color.Yellow, "▓ "},
{color.Magenta, "▓ "},
{color.Green, "▓ "},
{color.Cyan, "▓ "},
}
// Stdout ...
type Stdout struct {
withTimestamp bool
withTimestampNano bool
withHost bool
withPath bool
withTag bool
withoutMark bool
hFmt string
tFmt string
noColor bool
}
// NewStdout ...
func NewStdout(withTimestamp bool,
withTimestampNano bool,
withHost bool,
withPath bool,
withTag bool,
withoutMark bool,
hLen int,
tLen int,
noColor bool,
) (*Stdout, error) {
return &Stdout{
withTimestamp: withTimestamp,
withTimestampNano: withTimestampNano,
withHost: withHost,
withPath: withPath,
withTag: withTag,
withoutMark: withoutMark,
hFmt: fmt.Sprintf("%%-%ds ", hLen),
tFmt: fmt.Sprintf("%%-%ds ", tLen),
noColor: noColor,
}, nil
}
// Out ...
func (s *Stdout) Out(logChan chan parser.Log, hosts []string) {
if s.noColor {
color.Disable()
} else {
color.Enable()
}
for log := range logChan {
var (
bar string
ts string
filledByPrevTs string
host string
tag string
)
colorFunc := func(msg interface{}, styles ...string) string {
return msg.(string)
}
if s.withTimestamp {
if log.Timestamp == nil {
ts = fmt.Sprintf(fmt.Sprintf("%%-%ds", len(tsParseFmt)), "-")
} else {
ts = log.Timestamp.Format(tsParseFmt)
}
}
if s.withTimestampNano {
if log.Timestamp == nil {
ts = fmt.Sprintf(fmt.Sprintf("%%-%ds", len(tsNanoParseFmt)), "-")
} else {
ts = log.Timestamp.Format(tsNanoParseFmt)
}
}
if s.withTimestamp || s.withTimestampNano {
if log.FilledByPrevTs {
filledByPrevTs = "* "
} else {
filledByPrevTs = " "
}
}
if s.withHost && s.withPath {
host = fmt.Sprintf(s.hFmt, fmt.Sprintf("%s:%s", log.Host, log.Path))
} else if s.withHost {
host = fmt.Sprintf(s.hFmt, log.Host)
} else if s.withPath {
host = fmt.Sprintf(s.hFmt, log.Path)
}
if s.withTag {
tag = fmt.Sprintf(s.tFmt, fmt.Sprintf("%v", log.Target.Tags))
}
if s.withTimestamp || s.withTimestampNano || s.withHost || s.withPath {
for i, h := range hosts {
if h == log.Host {
colorFunc = colorizeMap[i%len(colorizeMap)].colorFunc
if s.withoutMark {
bar = ""
} else {
bar = colorFunc(colorizeMap[i%len(colorizeMap)].bar)
}
}
}
}
fmt.Printf("%s%s%s%s%s%s\n", bar, colorFunc(ts), color.White(filledByPrevTs, color.B), colorizeTag(colorFunc, tag), color.Grey(host), log.Content)
}
}
func colorizeTag(colorFunc func(interface{}, ...string) string, tag string) string {
colorized := []string{}
tags := strings.Split(tag, " ")
for _, t := range tags {
colorized = append(colorized, colorFunc(t, color.B))
}
return strings.Join(colorized, " ")
}