-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrollbarLogger.js
44 lines (40 loc) · 1.46 KB
/
rollbarLogger.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
import Rollbar from 'rollbar';
import omit from 'lodash/omit';
import isFunction from 'lodash/isFunction';
import { bunyanLevelToRollbarLevelName } from '../common/rollbar';
/**
* @description Custom bunyan stream that transports to Rollbar from a node process.
* See https://rollbar.com/docs/notifier/node_rollbar/ for integration details
*
* @param {Object} token, codeVersion, and environment
*/
export default function ServerRollbarLogger({ token, codeVersion, environment }) {
// https://rollbar.com/docs/notifier/rollbar.js/#quick-start-server
this._rollbar = new Rollbar({
accessToken: token,
captureUncaught: true,
captureUnhandledRejections: true,
code_version: codeVersion,
environment,
});
}
/**
* Transport to Rollbar
* @description handles `err` and `req` properties, attaches any custom data,
* and calls the appropriate Rollbar method.
*
* @param {Object} data
* @returns {undefined}
*/
ServerRollbarLogger.prototype.write = function (data = {}) {
const rollbarLevelName = bunyanLevelToRollbarLevelName(data.level);
const scopeData = omit(data, ['req', 'err', 'level']);
const payload = Object.assign({ level: rollbarLevelName }, scopeData);
// https://rollbar.com/docs/notifier/rollbar.js/#rollbarlog-1
const logFn = this._rollbar[rollbarLevelName];
if (isFunction(logFn)) {
logFn.call(this._rollbar, data.msg, data.err, data.req, payload);
} else {
this._rollbar.error(data.msg, data.err, data.req, payload);
}
};