forked from medicalwei/logrus_gcloud_formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logrus_gcloud_formatter.go
60 lines (50 loc) · 1.28 KB
/
logrus_gcloud_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
60
package logrus_gcloud_formatter
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
)
type LogrusGoogleCloudFormatter struct {
Type string // if not empty use for logstash type field.
// TimestampFormat sets the format used for timestamps.
TimestampFormat string
}
func levelToString(level logrus.Level) string {
switch level {
case logrus.DebugLevel:
return "debug"
case logrus.InfoLevel:
return "info"
case logrus.WarnLevel:
return "warning"
case logrus.ErrorLevel:
return "error"
case logrus.FatalLevel:
return "critical"
case logrus.PanicLevel:
return "critical"
}
return "info"
}
func (f *LogrusGoogleCloudFormatter) Format(entry *logrus.Entry) ([]byte, error) {
fields := make(logrus.Fields)
for k, v := range entry.Data {
switch v := v.(type) {
case error:
fields[k] = v.Error()
default:
fields[k] = v
}
}
epoch := entry.Time.Unix()
epoch_micros := epoch*int64(1000000) + int64(entry.Time.Nanosecond()/1000)
fields["timestamp_micros"] = epoch_micros
fields["timestamp"] = epoch
fields["message"] = entry.Message
fields["severity"] = levelToString(entry.Level)
serialized, err := json.Marshal(fields)
if err != nil {
return nil, fmt.Errorf("Failed to marshal fields to JSON, %v", err)
}
return append(serialized, '\n'), nil
}