-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathsentry-transport.js
107 lines (90 loc) · 2.29 KB
/
sentry-transport.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
var util = require('util'),
Raven = require('raven'),
winston = require('winston'),
_ = require('lodash');
var Sentry = winston.transports.Sentry = function (options) {
winston.Transport.call(this, _.pick(options, "level"));
// Default options
this.defaults = {
dsn: '',
patchGlobal: false,
logger: 'root',
levelsMap: {
silly: 'debug',
verbose: 'debug',
info: 'info',
debug: 'debug',
warn: 'warning',
error: 'error'
},
tags: {},
extra: {}
}
// For backward compatibility with deprecated `globalTags` option
options.tags = options.tags || options.globalTags;
this.options = _.defaultsDeep(options, this.defaults);
Raven.config(this.options.dsn, this.options);
if (this.options.patchGlobal) {
Raven.install();
}
// Handle errors
Raven.on('error', function(error) {
var message = "Cannot talk to sentry.";
if (error && error.reason) {
message += " Reason: " + error.reason;
}
console.log(message);
});
};
//
// Inherit from `winston.Transport` so you can take advantage
// of the base functionality and `.handleExceptions()`.
//
util.inherits(Sentry, winston.Transport);
//
// Expose the name of this Transport on the prototype
Sentry.prototype.name = 'sentry';
//
Sentry.prototype.log = function (level, msg, meta, callback) {
level = this.options.levelsMap[level];
meta = meta || {};
var extraData = _.extend({}, meta),
tags = extraData.tags;
delete extraData.tags;
var extra = {
'level': level,
'extra': extraData,
'tags': tags
};
if (extraData.request) {
extra.request = extraData.request;
delete extraData.request;
}
if (extraData.user) {
extra.user = extraData.user;
delete extraData.user;
}
try {
if (level == 'error') {
// Support exceptions logging
if (meta instanceof Error) {
if (msg == '') {
msg = meta;
} else {
meta.message = msg + ". cause: " + meta.message;
msg = meta;
}
}
Raven.captureException(msg, extra, function() {
callback(null, true);
});
} else {
Raven.captureMessage(msg, extra, function() {
callback(null, true);
});
}
} catch(err) {
console.error(err);
}
};
module.exports = Sentry;