Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save doublejosh/938017eb4b3aab922dce to your computer and use it in GitHub Desktop.

Select an option

Save doublejosh/938017eb4b3aab922dce to your computer and use it in GitHub Desktop.
Log front-end events to New Relic and the browser as desired by environment.
// Logging "constants"
nameSpace.watchLogDEBUG = 7;
nameSpace.watchLogNOTICE = 5;
nameSpace.watchLogWARNING = 4;
nameSpace.watchLogERROR = 3;
/**
* Log events.
*
* EXAMPLES:
* nameSpace.watchLog('Should know when this happens');
* nameSpace.watchLog('Should not happen', nameSpace.watchLogERROR);
* nameSpace.watchLog('Not great if this happens', nameSpace.watchLogWARNING, 'someFeature', {
* path: nameSpace.util.parseUrl(window.location.href).path,
* linkText: $(this).text()
* });
*
* @param {string} message
* Messages are intended more for simple NR Browser entries.
* @param {int} severtity (optional)
* Severity of the event, values:
* nameSpace.watchLogDEBUG - developer helper,
* nameSpace.watchLogNOTICE - nice to know about,
* nameSpace.watchLogWARNING - need to know when occurs,
* nameSpace.watchLogERROR - should never happen
* @param {string} type (optional)
* Type of item for easy lookup later.
* @param {mixed} data (optional)
* Data to stash with the log event.
*/
nameSpace.watchLog = function watchLog(message, severity, type, data) {
var settings = Site.settings.watchLog,
err;
// Defaults, and report threshold.
type = type || 'watchLog';
severity = severity || nameSpace.watchLogNOTICE;
if (severity <= settings.threshold) {
// Output to the browser.
if (settings.debuging && window.console) {
console.log(type + ': ' + severity + ' | ' + message);
if (data) console.log(data);
}
// Send to NR Browser.
if (window.NREUM) {
err = new Error(type + ' (' + severity + ') ' + message);
NREUM.noticeError(err);
}
// Send to NR Ingights.
_.extend(data || {}, {severity: severity, message: message});
nameSpace.insightsLogger(type, data);
}
};
/**
* Send events directly to NR Ingights.
*
* @param {object} data
* Data to insert into Insights. Properties will be query-able!
* @param {string} type (optoinal)
* Action Name within Insights. Default: logger.
*/
nameSpace.insightsLogger = function insightsLogger(data, type) {
if (window.newrelic) {
type = type || 'logger';
newrelic.addPageAction(type, data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment