-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatters.go
69 lines (53 loc) · 1.44 KB
/
formatters.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 epilog
import (
"bytes"
"encoding/json"
"fmt"
"strings"
)
// IFormatter formatters convert entries to []byte and used by handlers
type IFormatter interface {
Format(entry *Entry) []byte
}
// TextFormatter simple formatter
type TextFormatter struct {
format string
}
// NewTextFormatter creates new TextFormatter
func NewTextFormatter(format string) *TextFormatter {
return &TextFormatter{format: format}
}
// Format formats Entry
func (f *TextFormatter) Format(entry *Entry) []byte {
result := f.format
additionalBuf := &bytes.Buffer{}
data := filterEntryFields(entry)
if marshaledData, err := json.Marshal(data); err == nil {
additionalBuf.Write(marshaledData)
}
replaces := make([]string, 0, 2+len(entry.Fields))
replaces = append(
replaces,
":level:", entry.Level.String(),
":time:", entry.Time.UTC().Format("2006-01-02T15:04:05.000000-07:00"),
":message:", entry.Message,
":additional:", additionalBuf.String(),
)
for key, value := range entry.Fields {
replaces = append(replaces, fmt.Sprintf(":%s:", key), fmt.Sprintf("%s", value))
}
replacer := strings.NewReplacer(replaces...)
buf := &bytes.Buffer{}
replacer.WriteString(buf, result)
buf.WriteByte('\n')
return buf.Bytes()
}
func filterEntryFields(entry *Entry) map[string]interface{} {
result := make(map[string]interface{}, len(entry.Fields))
for key, value := range entry.Fields {
if key[0] != '_' {
result[key] = value
}
}
return result
}