-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
141 lines (117 loc) · 3.09 KB
/
formatter.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
package formatter
import (
"fmt"
"strings"
"text/tabwriter"
"time"
"github.com/thalesfsp/sypl/flag"
"github.com/thalesfsp/sypl/message"
"github.com/thalesfsp/sypl/processor"
"github.com/thalesfsp/sypl/shared"
)
// IFormatter specifies what a Formatter does.
type IFormatter = processor.IProcessor
// mapBuilder is a helper function to build the JSON map.
//
//nolint:ifshort
func mapBuilder(m message.IMessage) map[string]interface{} {
mM := map[string]interface{}{}
mM["id"] = m.GetID()
mM["contentBasedHashID"] = m.GetContentBasedHashID()
mM["component"] = m.GetComponentName()
mM["output"] = m.GetOutputName()
mM["level"] = strings.ToLower(m.GetLevel().String())
mM["timestamp"] = m.GetTimestamp().Format(time.RFC3339)
mM["message"] = m.GetContent().GetProcessed()
tags := m.GetTags()
if len(tags) != 0 {
mM["tags"] = tags
}
flg := m.GetFlag()
if flg != flag.None {
mM["flag"] = flg
}
outputsNames := m.GetOutputsNames()
if len(outputsNames) != 0 {
mM["outputsNames"] = outputsNames
}
processorsNames := m.GetProcessorsNames()
if len(processorsNames) != 0 {
mM["processorsNames"] = processorsNames
}
// Should only process fields if any.
if len(m.GetFields()) != 0 {
for k, v := range m.GetFields() {
if v != nil {
mM[k] = v
}
}
}
return mM
}
//////
// Built-in processors.
//////
// JSONPretty is a prettified-JSON formatter. It automatically adds:
// - Component
// - Level
// - Message
// - Output
// - Tags
// - Timestamp (RFC3339).
// - Fields.
func JSONPretty() IFormatter {
return processor.New("JSON", func(m message.IMessage) error {
m.GetContent().SetProcessed(shared.Prettify(mapBuilder(m)))
return nil
})
}
// JSON is a JSON formatter. It automatically adds:
// - Component
// - Level
// - Message
// - Output
// - Tags
// - Timestamp (RFC3339).
// - Fields.
func JSON() IFormatter {
return processor.New("JSON", func(m message.IMessage) error {
m.GetContent().SetProcessed(shared.Inline(mapBuilder(m)))
return nil
})
}
// Text is a text formatter. It automatically adds:
// - Component
// - Level
// - Message
// - Output
// - Timestamp (RFC3339).
// - Fields.
func Text() IFormatter {
return processor.New("Text", func(m message.IMessage) error {
buf := new(strings.Builder)
// Observe that the third line has no trailing tab,
// so its final cell is not part of an aligned column.
w := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
fmt.Fprintf(w, "component=%s\t", m.GetComponentName())
fmt.Fprintf(w, "output=%s\t", strings.ToLower(m.GetOutputName()))
fmt.Fprintf(w, "level=%s\t", strings.ToLower(m.GetLevel().String()))
fmt.Fprintf(w, "message=%s\t", m.GetContent().GetProcessed())
fmt.Fprintf(w, "timestamp=%s\t", m.GetTimestamp().Format(time.RFC3339))
// Should only process fields if any.
if len(m.GetFields()) != 0 {
for k, v := range m.GetFields() {
if v != nil {
fmt.Fprintf(w, "%s=%v\t", k, v)
}
}
}
// Should only process tags if any.
if len(m.GetTags()) != 0 {
fmt.Fprintf(w, "tags=[%s]", strings.Join(m.GetTags(), ", "))
}
w.Flush()
m.GetContent().SetProcessed(buf.String())
return nil
})
}