-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.go
57 lines (50 loc) · 1.37 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
// Package formatter provides a custom Logrus formatter for structuring logs into
// a data model.
package formatter
import (
"strings"
"github.com/sirupsen/logrus"
)
// LogrusDataModelFormatter is a custom formatter for Logrus that organizes log fields
// into structured groups and formats the output as JSON.
type LogrusDataModelFormatter struct {
BaseFormatter logrus.Formatter
}
func (l *LogrusDataModelFormatter) Format(entry *logrus.Entry) ([]byte, error) {
dataCopy := make(logrus.Fields)
for k, v := range entry.Data {
dataCopy[k] = v
}
ddFields := make(map[string]interface{})
customFields := make(map[string]interface{})
for k, v := range dataCopy {
if key, found := strings.CutPrefix(k, "dd."); found {
ddFields[key] = v
delete(dataCopy, k)
} else {
customFields[k] = v
delete(dataCopy, k)
}
}
if len(ddFields) > 0 {
dataCopy["dd"] = ddFields
}
if len(customFields) > 0 {
dataCopy["custom"] = customFields
}
jsonFormatter := &logrus.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z", // ISO 8601 format with milliseconds.
FieldMap: logrus.FieldMap{
logrus.FieldKeyTime: "timestamp",
logrus.FieldKeyMsg: "message",
},
DisableHTMLEscape: true,
}
return jsonFormatter.Format(&logrus.Entry{
Logger: entry.Logger,
Data: dataCopy,
Time: entry.Time.UTC(),
Level: entry.Level,
Message: entry.Message,
})
}