-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
104 lines (86 loc) · 2.5 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
const {
defaults,
defaultsDeep,
isFunction,
omit,
has,
} = require('lodash');
const Raven = require('raven');
const TransportStream = require('winston-transport');
function errorHandler(err) {
console.error(err);
}
class Sentry extends TransportStream {
constructor(opts = {}) {
super(opts);
this.name = 'winston-sentry-raven';
const options = Object.assign({}, opts);
defaultsDeep(options, {
dsn: process.env.SENTRY_DSN || '',
config: {
logger: 'winston-sentry-raven',
captureUnhandledRejections: false,
},
errorHandler,
install: false,
name: 'winston-sentry-raven',
silent: false,
level: 'info',
levelsMap: {
silly: 'debug',
verbose: 'debug',
info: 'info',
debug: 'debug',
warn: 'warning',
error: 'error',
},
});
this.levelsMap = options.levelsMap;
if (options.tags) {
options.config.tags = options.tags;
} else if (options.globalTags) {
options.config.tags = options.globalTags;
}
if (options.extra) {
options.config.extra = options.config.extra || {};
options.config.extra = defaults(options.config.extra, options.extra);
}
// expose the instance on the transport
this.raven = options.raven || Raven.config(options.dsn, options.config);
if (isFunction(options.errorHandler) && this.raven.listeners('error').length === 0) {
this.raven.on('error', options.errorHandler);
}
// it automatically will detect if it's already installed
if (options.install || options.patchGlobal) {
this.raven.install();
}
}
log(info, callback) {
const { message, stack, level, } = info;
const meta = Object.assign({}, omit(info, ['level', 'message', 'stack', 'label']));
setImmediate(() => {
this.emit('logged', level);
});
if (this.silent) return callback(null, true);
if (!has(this.levelsMap, level)) {
return callback(null, true);
}
const context = {};
context.level = this.levelsMap[level];
context.extra = meta;
if (context.level === 'error' || context.level === 'fatal') {
let exception = message;
if (stack) {
exception = new Error(message);
exception.stack = stack;
}
return this.raven.captureException(exception, context, () => {
callback(null, true);
});
}
return this.raven.captureMessage(message, context, () => {
callback(null, true);
});
}
}
module.exports = Sentry;