-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (51 loc) · 1.64 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
import { EOL } from 'os'
import build from 'pino-abstract-transport'
const SEVERITY = {
10: 'DEBUG',
20: 'DEBUG',
30: 'INFO',
40: 'WARNING',
50: 'ERROR',
60: 'CRITICAL',
}
export default async function (opts) {
return build(async function (source) {
for await (const obj of source) {
try {
const { level, time, msg, err, req, res, responseTime } = obj
delete obj.pid
delete obj.hostname
delete obj.level
obj.severity = SEVERITY[level]
delete obj.time
if (time) obj.timestamp = new Date(time).toISOString()
delete obj.msg
if (msg) obj.message = msg
// https://cloud.google.com/error-reporting/docs/formatting-error-messages#json_representation
if (err) {
delete obj.err
obj.stack_trace = err.stack
}
// Support pino-http logging.
if (req || res) {
delete obj.req
delete obj.res
delete obj.responseTime
obj.httpRequest = {}
obj.httpRequest.requestMethod = req.method
obj.httpRequest.requestUrl = req.url
obj.httpRequest.requestSize = req.headers['content-length']
obj.httpRequest.userAgent = req.headers['user-agent']
obj.httpRequest.remoteIp = req.remoteAddress
obj.httpRequest.status = res.statusCode
obj.httpRequest.responseSize = res.headers['content-length']
obj.httpRequest.latency = `${responseTime / 1000.0}s`
}
} catch (err) {
console.error(err, 'logging transport failed')
} finally {
process.stdout.write(JSON.stringify(obj) + EOL)
}
}
})
}