forked from guzru/winston-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentry-transport.js
99 lines (82 loc) · 2.27 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
var util = require('util'),
raven = require('raven'),
winston = require('winston'),
_ = require('underscore');
var Sentry = winston.transports.Sentry = function (options) {
this._dsn = options.dsn || '';
this._globalTags = options.globalTags || {};
this.patchGlobal = options.patchGlobal || false;
this._sentry = options.raven || new raven.Client(this._dsn, {logger: options.logger || 'root'});
if(this.patchGlobal) {
this._sentry.patchGlobal();
}
this._levels_map = options.levels_map || {
silly: 'debug',
verbose: 'debug',
info: 'info',
debug: 'debug',
warn: 'warning',
error: 'error'
}
// Set the level from your options
this.level = options.level || 'info';
// Handle errors
this._sentry.on('error', function() {
console.log("Cannot talk to sentry!");
});
// Expose sentry client to winston.Logger
winston.Logger.prototype.sentry_client = this._sentry;
};
//
// 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) {
// TODO: handle this better
level = this._levels_map[level] || this.level;
meta = meta || {};
var extraData = _.extend({}, meta),
tags = _.extend({}, this._globalTags, 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;
}
}
this._sentry.captureError(msg, extra, function() {
callback(null, true);
});
} else {
this._sentry.captureMessage(msg, extra, function() {
callback(null, true);
});
}
} catch(err) {
console.error(err);
}
};
module.exports = Sentry;