-
Notifications
You must be signed in to change notification settings - Fork 0
/
gceformatter.go
133 lines (115 loc) · 2.99 KB
/
gceformatter.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
123
124
125
126
127
128
129
130
131
132
133
package logrusgce
import (
"encoding/json"
"errors"
"fmt"
"runtime"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
)
type severity string
const (
logrusToCallerSkip = 5
)
const (
severityDEBUG severity = "DEBUG"
severityINFO severity = "INFO"
severityNOTICE severity = "NOTICE"
severityWARNING severity = "WARNING"
severityERROR severity = "ERROR"
severityCRITICAL severity = "CRITICAL"
severityALERT severity = "ALERT"
severityEMERGENCY severity = "EMERGENCY"
)
var (
levelsLogrusToGCE = map[logrus.Level]severity{
logrus.DebugLevel: severityDEBUG,
logrus.InfoLevel: severityINFO,
logrus.WarnLevel: severityWARNING,
logrus.ErrorLevel: severityERROR,
logrus.FatalLevel: severityCRITICAL,
logrus.PanicLevel: severityALERT,
}
)
var (
stackSkipsCallers = make([]uintptr, 0, 20)
stackSkips = map[logrus.Level]int{}
stackSkipsMu = sync.RWMutex{}
)
var (
ErrSkipNotFound = errors.New("could not find skips for log level")
)
type sourceLocation struct {
File string `json:"file"`
Line int `json:"line"`
FunctionName string `json:"functionName"`
}
func getSkipLevel(level logrus.Level) (int, error) {
stackSkipsMu.RLock()
if skip, ok := stackSkips[level]; ok {
defer stackSkipsMu.RUnlock()
return skip, nil
}
stackSkipsMu.RUnlock()
stackSkipsMu.Lock()
defer stackSkipsMu.Unlock()
if skip, ok := stackSkips[level]; ok {
return skip, nil
}
// detect until we escape logrus back to the client package
// skip out of runtime and logrusgce package, hence 3
stackSkipsCallers := make([]uintptr, 20)
runtime.Callers(3, stackSkipsCallers)
for i, pc := range stackSkipsCallers {
f := runtime.FuncForPC(pc)
if strings.HasPrefix(f.Name(), "github.com/sirupsen/logrus") == true {
continue
}
stackSkips[level] = i + 1
return i + 1, nil
}
return 0, ErrSkipNotFound
}
type GCEFormatter struct {
withSourceInfo bool
}
func NewGCEFormatter(withSourceInfo bool) *GCEFormatter {
return &GCEFormatter{withSourceInfo: withSourceInfo}
}
func (f *GCEFormatter) 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
}
}
data["time"] = entry.Time.Format(time.RFC3339Nano)
data["severity"] = levelsLogrusToGCE[entry.Level]
data["logMessage"] = entry.Message
if f.withSourceInfo == true {
skip, err := getSkipLevel(entry.Level)
if err != nil {
return nil, err
}
if pc, file, line, ok := runtime.Caller(skip); ok {
f := runtime.FuncForPC(pc)
data["sourceLocation"] = map[string]interface{}{
"file": file,
"line": line,
"functionName": f.Name(),
}
}
}
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
}