-
Notifications
You must be signed in to change notification settings - Fork 557
/
plain.go
197 lines (176 loc) · 4.53 KB
/
plain.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package logger
import (
"bytes"
"encoding/json"
"fmt"
"hash/adler32"
"io"
"sort"
"strings"
"time"
"github.com/mitchellh/colorstring"
"github.com/rs/zerolog"
)
var colorize = colorstring.Colorize{
Colors: colorstring.DefaultColors,
Reset: true,
}
type PlainOutput struct {
Out io.Writer
}
func (c *PlainOutput) Write(p []byte) (int, error) {
event := map[string]interface{}{}
d := json.NewDecoder(bytes.NewReader(p))
if err := d.Decode(&event); err != nil {
return 0, fmt.Errorf("cannot decode event: %s", err)
}
source := parseSource(event)
return fmt.Fprintln(c.Out, colorize.Color(fmt.Sprintf("%s %s %s%s%s",
formatTimestamp(event),
formatLevel(event),
formatSource(source),
formatMessage(event),
formatFields(event),
)))
}
func formatLevel(event map[string]interface{}) string {
level := zerolog.DebugLevel
if l, ok := event[zerolog.LevelFieldName].(string); ok {
level, _ = zerolog.ParseLevel(l)
}
switch level {
case zerolog.TraceLevel:
return "[magenta]TRC[reset]"
case zerolog.DebugLevel:
return "[yellow]DBG[reset]"
case zerolog.InfoLevel:
return "[green]INF[reset]"
case zerolog.WarnLevel:
return "[red]WRN[reset]"
case zerolog.ErrorLevel:
return "[red]ERR[reset]"
case zerolog.FatalLevel:
return "[red]FTL[reset]"
case zerolog.PanicLevel:
return "[red]PNC[reset]"
default:
return "[bold]???[reset]"
}
}
func formatTimestamp(event map[string]interface{}) string {
ts, ok := event[zerolog.TimestampFieldName].(string)
if !ok {
return "???"
}
t, err := time.Parse(zerolog.TimeFieldFormat, ts)
if err != nil {
panic(err)
}
return fmt.Sprintf("[dark_gray]%s[reset]", t.Format(time.Kitchen))
}
func formatMessage(event map[string]interface{}) string {
message, ok := event[zerolog.MessageFieldName].(string)
if !ok {
return ""
}
message = strings.TrimSpace(message)
if err, ok := event[zerolog.ErrorFieldName].(string); ok && err != "" {
message = message + ": " + err
}
level := zerolog.DebugLevel
if l, ok := event[zerolog.LevelFieldName].(string); ok {
level, _ = zerolog.ParseLevel(l)
}
switch level {
case zerolog.TraceLevel:
return fmt.Sprintf("[dim]%s[reset]", message)
case zerolog.DebugLevel:
return fmt.Sprintf("[dim]%s[reset]", message)
case zerolog.InfoLevel:
return message
case zerolog.WarnLevel:
return fmt.Sprintf("[yellow]%s[reset]", message)
case zerolog.ErrorLevel:
return fmt.Sprintf("[red]%s[reset]", message)
case zerolog.FatalLevel:
return fmt.Sprintf("[red]%s[reset]", message)
case zerolog.PanicLevel:
return fmt.Sprintf("[red]%s[reset]", message)
default:
return message
}
}
func parseSource(event map[string]interface{}) string {
source := "system"
if task, ok := event["component"].(string); ok && task != "" {
source = task
}
return source
}
func formatSource(source string) string {
return fmt.Sprintf("[%s]%s | [reset]",
hashColor(source),
source,
)
}
func formatFields(entry map[string]interface{}) string {
// these are the fields we don't want to expose, either because they're
// already part of the Log structure or because they're internal
fieldSkipList := map[string]struct{}{
zerolog.MessageFieldName: {},
zerolog.LevelFieldName: {},
zerolog.TimestampFieldName: {},
zerolog.ErrorFieldName: {},
zerolog.CallerFieldName: {},
"environment": {},
"component": {},
"state": {},
}
fields := []string{}
for key, value := range entry {
if _, ok := fieldSkipList[key]; ok {
continue
}
switch v := value.(type) {
case string:
fields = append(fields, fmt.Sprintf("%s=%s", key, v))
case int:
fields = append(fields, fmt.Sprintf("%s=%v", key, v))
case float64:
dur := time.Duration(v) * time.Millisecond
s := dur.Round(100 * time.Millisecond).String()
fields = append(fields, fmt.Sprintf("%s=%s", key, s))
case nil:
fields = append(fields, fmt.Sprintf("%s=null", key))
default:
o, err := json.MarshalIndent(v, "", " ")
if err != nil {
panic(err)
}
fields = append(fields, fmt.Sprintf("%s=%s", key, o))
}
}
if len(fields) == 0 {
return ""
}
sort.SliceStable(fields, func(i, j int) bool {
return fields[i] < fields[j]
})
return fmt.Sprintf(" [bold]%s[reset]", strings.Join(fields, " "))
}
// hashColor returns a consistent color for a given string
func hashColor(text string) string {
colors := []string{
"green",
"light_green",
"light_blue",
"blue",
"magenta",
"light_magenta",
"light_yellow",
"cyan",
"light_cyan",
}
h := adler32.Checksum([]byte(text))
return colors[int(h)%len(colors)]
}