forked from kdar/logrus-cloudwatchlogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prod_formatter.go
269 lines (233 loc) · 6.25 KB
/
prod_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package logrus_cloudwatchlogs
import (
"bytes"
"encoding/json"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/kardianos/osext"
"github.com/sirupsen/logrus"
)
type ProdFormatter struct {
hostname string
appname string
httpRequestKey string
httpRequestHeaderFilter []string
}
type ProdFormatterOption func(*ProdFormatter) error
//Marshaler is an interface any type can implement to change its output in our production logs.
type Marshaler interface {
MarshalLog() map[string]interface{}
}
// Hostname is a formatter option that specifies the hostname this
// program is running on. If this is not specified, the system's hostname
// will be used.
func Hostname(name string) ProdFormatterOption {
return func(f *ProdFormatter) error {
f.hostname = name
return nil
}
}
// AppName is a formatter option that specifies the name of the app.
// If this is not specified, the default is to use the executable name.
func AppName(name string) ProdFormatterOption {
return func(f *ProdFormatter) error {
f.appname = name
return nil
}
}
// HTTPRequest is a formatter option that allows you to indicate
// that a certain key will contain an *http.Request. If it does, it
// will be formatted in the output. You can provide an optional list
// of keys to filter out of the serialized header. This is useful so
// you don't include sensitive information (like the authorization header).
// Note: if you do not provide this and you pass in an *http.Request, it will
// fail because encoding/json cannot serialize *http.Request.
func HTTPRequest(key string, headerFilter ...string) ProdFormatterOption {
return func(f *ProdFormatter) error {
f.httpRequestKey = key
f.httpRequestHeaderFilter = headerFilter
return nil
}
}
// NewProdFormatter creates a new cloudwatchlogs production formatter.
// This is opinionated and you can feel free to create your own.
func NewProdFormatter(options ...ProdFormatterOption) *ProdFormatter {
f := &ProdFormatter{}
for _, option := range options {
option(f)
}
var err error
if f.hostname == "" {
if f.hostname, err = os.Hostname(); err != nil {
f.hostname = "unknown"
}
}
if f.appname == "" {
if f.appname, err = osext.Executable(); err == nil {
f.appname = filepath.Base(f.appname)
} else {
f.appname = "app"
}
}
return f
}
// Format formats logrus.Entry in the form of:
// [timestamp] [jsondata]
func (f *ProdFormatter) Format(entry *logrus.Entry) ([]byte, error) {
//tmp := make([]byte, 50)
b := &bytes.Buffer{}
// //b.WriteString(time.Now().Format("2006-01-02T15:04:05.999Z"))
now := time.Now()
// year, month, day := now.Date()
// hour, minute, second := now.Clock()
// nano := now.Nanosecond()
// fourDigits(&tmp, 0, year)
// tmp[4] = '-'
// twoDigits(&tmp, 5, int(month))
// tmp[7] = '-'
// twoDigits(&tmp, 8, day)
// tmp[10] = 'T'
// twoDigits(&tmp, 11, hour)
// tmp[13] = ':'
// twoDigits(&tmp, 14, minute)
// tmp[16] = ':'
// twoDigits(&tmp, 17, second)
// tmp[19] = '.'
// threeDigits(&tmp, 20, nano)
// tmp[23] = 'Z'
// b.Write(tmp[:24])
//
// b.WriteRune(' ')
// // This is so incredibly hacky. Needed until logrus implements
// // this.
// skip := 8
// if len(entry.Data) == 0 {
// skip = 9
// }
// file, line, function := fileInfo(skip)
data := map[string]interface{}{
"time": now.Unix(), //b.String()[:24],
"msg": entry.Message,
"level": entry.Level.String(),
"host": f.hostname,
// "file": file,
// "line": line,
// "func": function,
"app": f.appname,
}
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()
case Marshaler:
data[k] = v.MarshalLog()
default:
data[k] = v
}
}
if v, ok := data[f.httpRequestKey]; ok {
if req, ok := v.(*http.Request); ok {
header := make(map[string]interface{})
for key, value := range req.Header {
header[key] = value
}
for _, key := range f.httpRequestHeaderFilter {
delete(header, key)
}
data[f.httpRequestKey] = map[string]interface{}{
"method": req.Method,
// We have to use RequestURI because URL may be
// modified by routes.
"url": req.RequestURI, //t.URL.String(),
"host": req.Host,
"remote_addr": req.RemoteAddr,
"header": header,
}
}
}
j, err := json.Marshal(data)
if err != nil {
return nil, err
}
b.Write(j)
return b.Bytes(), nil
}
func fileInfo(skip int) (string, int, string) {
function := "<???>"
pc, file, line, ok := runtime.Caller(skip)
if !ok {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
me := runtime.FuncForPC(pc)
if me != nil {
function = me.Name()
}
}
return file, line, function
}
const ddigits = `0001020304050607080910111213141516171819` +
`2021222324252627282930313233343536373839` +
`4041424344454647484950515253545556575859` +
`6061626364656667686970717273747576777879` +
`8081828384858687888990919293949596979899`
// itoa converts an integer d to its ascii representation
// i is the deintation index in buf
// algorithm from https://www.facebook.com/notes/facebook-engineering/three-optimization-tips-for-c/10151361643253920
func itoa(buf *[]byte, i, d int) int {
j := len(*buf)
for d >= 100 {
// Integer division is slow, so we do it by 2
index := (d % 100) * 2
d /= 100
j--
(*buf)[j] = ddigits[index+1]
j--
(*buf)[j] = ddigits[index]
}
if d < 10 {
j--
(*buf)[j] = byte(int('0') + d)
return copy((*buf)[i:], (*buf)[j:])
}
index := d * 2
j--
(*buf)[j] = ddigits[index+1]
j--
(*buf)[j] = ddigits[index]
return copy((*buf)[i:], (*buf)[j:])
}
const digits = "0123456789"
// twoDigits converts an integer d to its ascii representation
// i is the destination index in buf
func twoDigits(buf *[]byte, i, d int) {
(*buf)[i+1] = digits[d%10]
d /= 10
(*buf)[i] = digits[d%10]
}
func threeDigits(buf *[]byte, i, d int) {
(*buf)[i+2] = digits[d%10]
d /= 10
(*buf)[i+1] = digits[d%10]
d /= 10
(*buf)[i] = digits[d%10]
}
func fourDigits(buf *[]byte, i, d int) {
(*buf)[i+3] = digits[d%10]
d /= 10
(*buf)[i+2] = digits[d%10]
d /= 10
(*buf)[i+1] = digits[d%10]
d /= 10
(*buf)[i] = digits[d%10]
}