/
formatter.go
59 lines (49 loc) · 1.47 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
package logger
import (
"encoding/json"
"errors"
"fmt"
"time"
"github.com/sirupsen/logrus"
)
const timestampFormat = time.RFC3339Nano
// ftJSONFormatter formats the logs in JSON format.
// It always includes "msg", "level" and "service_name" fields for each log entry.
// If there is time field in the log entry, ftJSONFormatter logs it in time.RFC3339Nano format.
type ftJSONFormatter struct {
serviceName string
keyConf *KeyNamesConfig
}
func newFTJSONFormatter(serviceName string, keyConf *KeyNamesConfig) *ftJSONFormatter {
return &ftJSONFormatter{serviceName: serviceName, keyConf: keyConf}
}
func (f *ftJSONFormatter) Format(entry *logrus.Entry) ([]byte, error) {
if f.serviceName == "" {
return []byte{}, errors.New("UPP log formatter is not initialised with service name")
}
data := make(logrus.Fields)
for k, v := range entry.Data {
switch v := v.(type) {
case error:
// Otherwise errors are ignored by `encoding/json`
data[k] = v.Error()
default:
if v != nil && v != "" {
data[k] = v
}
}
}
if _, found := data[f.keyConf.KeyTime]; !found {
data[f.keyConf.KeyTime] = entry.Time.Format(timestampFormat)
}
if entry.Message != "" {
data[f.keyConf.KeyMsg] = entry.Message
}
data[f.keyConf.KeyLogLevel] = entry.Level.String()
data[f.keyConf.KeyServiceName] = f.serviceName
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
}