forked from joaquimserafim/superagent-bunyan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (117 loc) · 2.87 KB
/
index.js
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
/*
eslint
no-multi-spaces: ["error", {exceptions: {"VariableDeclarator": true}}]
padded-blocks: ["error", {"classes": "always"}]
max-len: ["error", 80]
*/
'use strict'
const getPropValue = require('get-property-value')
const objectSize = require('object.size')
const url = require('url')
const isObject = require('is.object')
module.exports = logger
function logger (bunyan, requestId, extra) {
if (isObject(requestId)) {
extra = requestId
requestId = null
}
return runLogger
function runLogger (req) {
let appendRes = {}
let endTime = 0
const startTime = process.hrtime()
req.id = requestId || getPropValue(req.header, 'X-Request-ID') || id()
const log = bunyan
.child(Object
.assign({
origin: 'superagent',
req_id: req.id,
serializers: {
err: bunyan.constructor.stdSerializers.err,
req: reqSerializer,
res: resSerializer
}
},
isObject(extra) ? extra : {}
)
)
log.info({req: req}, 'start of the request')
req.once('end', onEnd)
req.once('error', onError)
req.once('response', onResponse)
return req
function onEnd () {
endTime = process.hrtime(startTime)
}
function onError (err) {
// if isn't a http error the onend will not be called
if (!endTime) {
endTime = getTimeMs(process.hrtime(startTime))
err = Object.assign(err, {opDuration: endTime})
}
log.error(
{
res: appendRes,
err: err,
duration: endTime
},
'end of the request'
)
}
// in case of res.error should fallback to the error emitter
// and should pass the res object
function onResponse (res) {
endTime = getTimeMs(endTime)
res = Object.assign(res, {opDuration: endTime})
if (res.error) {
appendRes = res
} else {
log.info(
{
res: res,
duration: endTime
},
'end of the request'
)
}
}
}
}
//
// help functions
//
function id () {
return new Date().getTime().toString(36) +
'-' +
(Math.random() * Math.pow(36, 8) << 0).toString(36)
}
function resSerializer (res) {
return {
statusCode: res.statusCode,
headers: res.headers,
body: objectSize(res.body) ? res.body : res.text
}
}
function reqSerializer (req) {
return {
method: req.method,
url: req.url,
qs: getQs(req),
path: req.url && url.parse(req.url).pathname,
body: req._data,
headers: req.header
}
}
function getTimeMs (endTime) {
return endTime[0] * 1e3 + endTime[1] * 1e-6
}
function getQs (req) {
return objectSize(req.qs)
? req.qs
: getRawQs(req)
}
function getRawQs (req) {
return req.qsRaw && req.qsRaw.length
? req.qsRaw.join('&')
: undefined
}