forked from marnusw/bunyan-bugsnag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (62 loc) · 1.65 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
const Stream = require('stream')
const bugsnag = require('bugsnag')
const levelFromName = {
trace: 10,
debug: 20,
info: 30,
warn: 40,
error: 50,
fatal: 60,
}
function bugsnagLogStream(options) {
options = normalizeLevels(options, {
systemInfo: ['name', 'hostname', 'pid', 'req_id', 'level', 'time', 'v'],
warningLevel: 40,
errorLevel: 50,
})
return new Stream.Writable({
objectMode: true,
write(data) {
if (typeof data === 'string') {
data = JSON.parse(data)
}
const error = data.err || data.error || new Error(data.msg)
delete data.error
delete data.err
const optionsMap = splitSystemAndInfo(data)
optionsMap.severity = selectSeverity(data.level)
bugsnag.notify(error, optionsMap)
return true
},
})
function normalizeLevels(opts, defaults) {
opts = Object.assign(defaults, opts)
if (typeof opts.warningLevel === 'string') {
opts.warningLevel = levelFromName[opts.warningLevel]
}
if (typeof opts.errorLevel === 'string') {
opts.errorLevel = levelFromName[opts.errorLevel]
}
return opts
}
function selectSeverity(level) {
if (level >= options.errorLevel) {
return 'error'
}
if (level >= options.warningLevel) {
return 'warning'
}
return 'info'
}
function splitSystemAndInfo(data) {
const systemInfo = options.systemInfo
const system = {}
const info = Object.assign({}, data)
for (let i = 0; i < systemInfo.length; ++i) {
system[systemInfo[i]] = data[systemInfo[i]]
delete info[systemInfo[i]]
}
return {system, info}
}
}
module.exports = bugsnagLogStream