Files
happy-app/server/commons/error-tracking.js
2017-12-30 14:25:32 +00:00

62 lines
1.5 KiB
JavaScript

const Rollbar = require('rollbar');
class ErrorTracking{
/**
* Create a rollbar instance, with API key and configure options
*/
constructor(){
// Get API key, check it was there, then create a new rollbar instance
const rollbarApiKey = process.env.ROLLBAR_KEY;
this.keySupplied = (!!rollbarApiKey);
if(this.keySupplied) {
this.rollbar = new Rollbar(rollbarApiKey);
// Set params
this.rollbar.captureUncaught = true;
// this.rollbar.configure({logLevel: "warning"}); // Uncomment to log only warnings, and up
// Don't track if tracking is disabled
if (!this.shouldBeTracking) {
this.rollbar.configure({enabled: false});
}
}
}
/**
* Contains the environmental calls to determine if we want to be tracking
* in the at the moment
* @returns {boolean}
*/
shouldBeTracking() {
return (process.env.NODE_ENV === "production" && this.keySupplied);
}
/**
* Log an info message to rollbar
* @param message
* @param details
*/
logMessage(message, details = null){
if (!this.shouldBeTracking()){ return null; }
else { this.rollbar.info(message, details); }
}
/**
* Log a warning to rollbar
* @param warning
* @param details
*/
logWarning(warning, details = null){
if (!this.shouldBeTracking()) return;
this.rollbar.warning(warning, details);
}
}
module.exports = new ErrorTracking();