-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_terminal.go
72 lines (57 loc) · 1.37 KB
/
encoder_terminal.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
package clogger
import (
"bytes"
"encoding/json"
"fmt"
"time"
)
var (
timeFormatTerm = "2006/01/02 - 15:04:05"
)
type TerminalEncoder struct{}
func (t *TerminalEncoder) EncodeLogEntry(entry *LogEntry) ([]byte, error) {
fields := make(map[string]interface{})
for _, field := range entry.fields.fields() {
fields[field.key] = field.value
}
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(fields)
out := fmt.Sprintf("%s\t %s\t %s | %s",
time.Now().Format(timeFormatTerm),
entry.severity,
entry.message,
buf.Bytes(),
)
return []byte(out), err
}
func (t *TerminalEncoder) EncodeEvent(event *Event) ([]byte, error) {
fields := make(map[string]interface{})
// Fields
for _, field := range event.fields.fields() {
fields["fields"] = map[string]interface{}{
field.key: field.value,
}
}
// Err fields
for _, field := range event.errFields.fields() {
fields["errors"] = map[string]interface{}{
field.key: field.value,
}
}
// Labels
for _, field := range event.labels.fields() {
fields["labels"] = map[string]interface{}{
field.key: field.value,
}
}
fields["elapsed"] = time.Since(event.timestamp).String()
buf := &bytes.Buffer{}
err := json.NewEncoder(buf).Encode(fields)
out := fmt.Sprintf("%s\t %s\t %s | %s",
time.Now().Format(timeFormatTerm),
event.severity,
event.message,
buf.Bytes(),
)
return []byte(out), err
}