forked from joonix/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluentd.go
122 lines (104 loc) · 2.63 KB
/
fluentd.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
package log
import (
"encoding/json"
"fmt"
"time"
"github.com/sirupsen/logrus"
)
// Default logrus to FluentD severity map
var SeverityMap = map[string]string{
"panic": "fatal",
"fatal": "fatal",
"warning": "warn",
"debug": "debug",
"error": "error",
"trace": "trace",
"info": "info",
}
// logrus to stackdriver severity map
func UseStackdriverSeverity() {
SeverityMap = map[string]string{
"panic": "CRITICAL",
"fatal": "CRITICAL",
"warning": "WARNING",
"debug": "DEBUG",
"error": "ERROR",
"trace": "DEBUG",
"info": "INFO",
}
}
// FluentdFormatter is similar to logrus.JSONFormatter but with log level that are recongnized
// by kubernetes fluentd.
type FluentdFormatter struct {
TimestampFormat string
SeverityMap map[string]string
}
// Format the log entry. Implements logrus.Formatter.
func (f *FluentdFormatter) Format(entry *logrus.Entry) ([]byte, error) {
data := make(logrus.Fields, len(entry.Data)+3)
for k, v := range entry.Data {
switch v := v.(type) {
case error:
// Otherwise errors are ignored by `encoding/json`
// https://github.com/Sirupsen/logrus/issues/137
data[k] = v.Error()
default:
data[k] = v
}
}
prefixFieldClashes(data, entry.HasCaller())
timestampFormat := f.TimestampFormat
if timestampFormat == "" {
timestampFormat = time.RFC3339Nano
}
data["timestamp"] = entry.Time.Format(timestampFormat)
data["message"] = entry.Message
if ms, ok := SeverityMap[entry.Level.String()]; ok {
data["severity"] = ms
} else {
data["severity"] = SeverityMap["debug"]
}
if entry.HasCaller() {
funcVal := entry.Caller.Function
fileVal := fmt.Sprintf("%s:%d", entry.Caller.File, entry.Caller.Line)
if funcVal != "" {
data[logrus.FieldKeyFunc] = funcVal
}
if fileVal != "" {
data[logrus.FieldKeyFile] = fileVal
}
}
serialized, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
}
return append(serialized, '\n'), nil
}
func prefixFieldClashes(data logrus.Fields, reportCaller bool) {
if t, ok := data["time"]; ok {
data["fields.time"] = t
}
if m, ok := data["msg"]; ok {
data["fields.msg"] = m
}
if l, ok := data["level"]; ok {
data["fields.level"] = l
}
if m, ok := data["message"]; ok {
data["fields.message"] = m
}
if l, ok := data["timestamp"]; ok {
data["fields.timestamp"] = l
}
if l, ok := data["severity"]; ok {
data["fields.severity"] = l
}
if reportCaller {
if l, ok := data[logrus.FieldKeyFunc]; ok {
data["fields."+logrus.FieldKeyFunc] = l
}
if l, ok := data[logrus.FieldKeyFile]; ok {
data["fields."+logrus.FieldKeyFile] = l
}
}
}