This repository has been archived by the owner on May 20, 2021. It is now read-only.
forked from TV4/logrus-stackdriver-formatter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
formatter.go
193 lines (164 loc) · 4.65 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package stackdriver
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"time"
"github.com/go-stack/stack"
"github.com/sirupsen/logrus"
)
var skipTimestamp bool
type severity string
const (
severityDebug severity = "DEBUG"
severityInfo severity = "INFO"
severityWarning severity = "WARNING"
severityError severity = "ERROR"
severityCritical severity = "CRITICAL"
severityAlert severity = "ALERT"
)
var levelsToSeverity = map[logrus.Level]severity{
logrus.DebugLevel: severityDebug,
logrus.InfoLevel: severityInfo,
logrus.WarnLevel: severityWarning,
logrus.ErrorLevel: severityError,
logrus.FatalLevel: severityCritical,
logrus.PanicLevel: severityAlert,
}
type serviceContext struct {
Service string `json:"service,omitempty"`
Version string `json:"version,omitempty"`
}
type reportLocation struct {
FilePath string `json:"filePath,omitempty"`
LineNumber int `json:"lineNumber,omitempty"`
FunctionName string `json:"functionName,omitempty"`
}
type context struct {
Data map[string]interface{} `json:"data,omitempty"`
ReportLocation *reportLocation `json:"reportLocation,omitempty"`
HTTPRequest map[string]interface{} `json:"httpRequest,omitempty"`
}
type entry struct {
Timestamp string `json:"timestamp,omitempty"`
ServiceContext *serviceContext `json:"serviceContext,omitempty"`
Message string `json:"message,omitempty"`
Severity severity `json:"severity,omitempty"`
Context *context `json:"context,omitempty"`
}
// Formatter implements Stackdriver formatting for logrus.
type Formatter struct {
Service string
Version string
StackSkip []string
}
// Option lets you configure the Formatter.
type Option func(*Formatter)
// WithService lets you configure the service name used for error reporting.
func WithService(n string) Option {
return func(f *Formatter) {
f.Service = n
}
}
// WithVersion lets you configure the service version used for error reporting.
func WithVersion(v string) Option {
return func(f *Formatter) {
f.Version = v
}
}
// WithStackSkip lets you configure which packages should be skipped for locating the error.
func WithStackSkip(v string) Option {
return func(f *Formatter) {
f.StackSkip = append(f.StackSkip, v)
}
}
// NewFormatter returns a new Formatter.
func NewFormatter(options ...Option) *Formatter {
fmtr := Formatter{
StackSkip: []string{
"github.com/sirupsen/logrus",
},
}
for _, option := range options {
option(&fmtr)
}
return &fmtr
}
func (f *Formatter) errorOrigin() (stack.Call, error) {
skip := func(pkg string) bool {
for _, skip := range f.StackSkip {
if pkg == skip {
return true
}
}
return false
}
// We start at 2 to skip this call and our caller's call.
for i := 2; ; i++ {
c := stack.Caller(i)
// ErrNoFunc indicates we're over traversing the stack.
if _, err := c.MarshalText(); err != nil {
return stack.Call{}, nil
}
pkg := fmt.Sprintf("%+k", c)
// Remove vendoring from package path.
parts := strings.SplitN(pkg, "/vendor/", 2)
pkg = parts[len(parts)-1]
if !skip(pkg) {
return c, nil
}
}
}
// Format formats a logrus entry according to the Stackdriver specifications.
func (f *Formatter) Format(e *logrus.Entry) ([]byte, error) {
severity := levelsToSeverity[e.Level]
ee := entry{
Message: e.Message,
Severity: severity,
Context: &context{
Data: e.Data,
},
}
if !skipTimestamp {
ee.Timestamp = time.Now().UTC().Format(time.RFC3339)
}
switch severity {
case severityError, severityCritical, severityAlert:
ee.ServiceContext = &serviceContext{
Service: f.Service,
Version: f.Version,
}
// When using WithError(), the error is sent separately, but Error
// Reporting expects it to be a part of the message so we append it
// instead.
if err, ok := ee.Context.Data["error"]; ok {
ee.Message = fmt.Sprintf("%s: %s", e.Message, err)
delete(ee.Context.Data, "error")
} else {
ee.Message = e.Message
}
// As a convenience, when using supplying the httpRequest field, it
// gets special care.
if reqData, ok := ee.Context.Data["httpRequest"]; ok {
if req, ok := reqData.(map[string]interface{}); ok {
ee.Context.HTTPRequest = req
delete(ee.Context.Data, "httpRequest")
}
}
// Extract report location from call stack.
if c, err := f.errorOrigin(); err == nil {
lineNumber, _ := strconv.ParseInt(fmt.Sprintf("%d", c), 10, 64)
ee.Context.ReportLocation = &reportLocation{
FilePath: fmt.Sprintf("%+s", c),
LineNumber: int(lineNumber),
FunctionName: fmt.Sprintf("%n", c),
}
}
}
b, err := json.Marshal(ee)
if err != nil {
return nil, err
}
return append(b, '\n'), nil
}