Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created July 3, 2013 18:37
Show Gist options
  • Save alexbeletsky/5921464 to your computer and use it in GitHub Desktop.
Save alexbeletsky/5921464 to your computer and use it in GitHub Desktop.
Logger.js
var _ = require('underscore');
var util = require('util');
var colors = require('colors');
var moment = require('moment');
var logentries = require('node-logentries');
var sinon = require('sinon');
var log = logentries.logger({
token:process.env.LOGENTRIES_TOKEN
});
log.level('info');
var logger = {
colorsMap: {
'success': 'green',
'warning': 'yellow',
'err': 'red',
'info': 'grey'
},
success: function (message) {
this.log('success', message);
},
warning: function (message) {
this.log('warning', message);
},
error: function (message) {
this.log('err', message);
},
info: function (message) {
this.log('info', message);
},
log: function (type, message) {
var record = this.timestamptMessage(util.format('%s: %s', type.toUpperCase(), this.formatMessage(message)));
console.log(record[this.colorsMap[type]]);
},
formatMessage: function (message) {
return typeof message === 'string' ? message : JSON.stringify(message);
},
timestamptMessage: function (message) {
return util.format('[%s] %s', moment(), message);
}
};
var logentriesLogger = (function (_super) {
var child = {
log: function (type, message) {
_super.log(type, message);
log.log(type, message);
}
};
return _.extend(Object.create(_super), child);
})(logger);
function loggerFactory (env) {
return env === 'test' ? sinon.stub (logentriesLogger) : logentriesLogger;
}
module.exports = loggerFactory(process.env.TEST_ENV);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment