Skip to content

Instantly share code, notes, and snippets.

@doublejosh
Last active April 21, 2016 02:08
Show Gist options
  • Select an option

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

Select an option

Save doublejosh/b5f33e5b42f9c84790ccca456707fb7b to your computer and use it in GitHub Desktop.
/**
* Template for adding plenty of easy logging to your code.
*/
var severityLevel = configs.severity || 1,
log = {};
// Quick messages, not intended to post unless debugging is needed.
log.debug = function (message, data) {
log.post(0, message, data);
}
// Good things to know, but just for info and monitoring.
log.notice = function (message, data) {
log.post(1, message, data);
}
// Events that should be dealt with, but nothing is broken.
log.warn = function (message, data) {
log.post(3, message, data);
}
// Problems that mean the system is broken somehow.
log.error = function (message, data) {
log.post(5, message, data);
}
// Push the event to the logging system.
log.post = function (severity, message, data) {
var Service = new LogService;
if (environment != 'production') {
console.log(message);
console.log(data);
}
if (severity < severityLevel) return;
data = data || {};
data.message = message;
Service.postAPI(host, Date.now(), severity, data);
}
/**
* Example use...
*/
log.warn('Expired account set to closed', {
'type': 'status-check',
'user': '123'
});
log.error('Status checking expired users failed to connect', {
'type': 'scheduled-task'
});
log.debug('Loading all expired users');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment