logger: added option to disable formatting

- systemd journalctl includes timestamps in log messages already
- updated logger to use console.error, console.warn, console.info, and etc.
This commit is contained in:
Braydon Fuller 2016-04-27 13:46:50 -04:00
parent ea792b692f
commit 24d1bc82e9
4 changed files with 109 additions and 11 deletions

View File

@ -1,14 +1,22 @@
'use strict';
var bitcore = require('bitcore-lib');
var _ = bitcore.deps._;
var colors = require('colors/safe');
/**
* Wraps console.log with some special magic
* @constructor
*/
function Logger() {
function Logger(options) {
if (!options) {
options = {};
}
this.formatting = _.isUndefined(options.formatting) ? Logger.DEFAULT_FORMATTING : options.formatting;
}
Logger.DEFAULT_FORMATTING = true;
/**
* Prints an info message
* #info
@ -46,16 +54,17 @@ Logger.prototype.warn = function() {
* #_log
*/
Logger.prototype._log = function(color) {
if (process.env.NODE_ENV === 'test') {
return;
}
var args = Array.prototype.slice.call(arguments);
args = args.slice(1);
var date = new Date();
var typeString = colors[color].italic(args.shift() + ':');
args[0] = '[' + date.toISOString() + ']' + ' ' + typeString + ' ' + args[0];
console.log.apply(console, args);
var level = args.shift();
if (this.formatting) {
var date = new Date();
var typeString = colors[color].italic(level + ':');
args[0] = '[' + date.toISOString() + ']' + ' ' + typeString + ' ' + args[0];
}
var fn = console[level] || console.log;
fn.apply(console, args);
};
module.exports = Logger;

View File

@ -27,6 +27,7 @@ var errors = require('./errors');
* ```
*
* @param {Object} config - The configuration of the node
* @param {Array} config.formatLogs - Option to disable formatting of logs
* @param {Array} config.services - The array of services
* @param {Number} config.port - The HTTP port for services
* @param {Boolean} config.https - Enable https
@ -43,6 +44,11 @@ function Node(config) {
this.configPath = config.path;
this.errors = errors;
this.log = log;
if (!_.isUndefined(config.formatLogs)) {
this.log.formatting = config.formatLogs ? true : false;
}
this.network = null;
this.services = {};
this._unloadedServices = [];

View File

@ -32,10 +32,10 @@
"scripts": {
"preinstall": "./scripts/download",
"verify": "./scripts/download --skip-bitcoin-download --verify-bitcoin-download",
"test": "NODE_ENV=test mocha -R spec --recursive",
"test": "mocha -R spec --recursive",
"regtest": "./scripts/regtest",
"jshint": "jshint --reporter=node_modules/jshint-stylish ./lib",
"coverage": "NODE_ENV=test istanbul cover _mocha -- --recursive"
"coverage": "istanbul cover _mocha -- --recursive"
},
"tags": [
"bitcoin",

83
test/logger.unit.js Normal file
View File

@ -0,0 +1,83 @@
'use strict';
var sinon = require('sinon');
var chai = require('chai');
var should = chai.should();
var Logger = require('../lib/logger');
describe('Logger', function() {
var sandbox = sinon.sandbox.create();
afterEach(function() {
sandbox.restore();
});
it('will instatiate without options', function() {
var logger = new Logger();
should.exist(logger);
logger.formatting.should.equal(true);
});
it('will instatiate with formatting option', function() {
var logger = new Logger({
formatting: false
});
logger.formatting.should.equal(false);
var logger2 = new Logger({
formatting: true
});
logger2.formatting.should.equal(true);
});
it('will log with formatting', function() {
var logger = new Logger({formatting: true});
sandbox.stub(console, 'info');
logger.info('Test info log');
console.info.callCount.should.equal(1);
console.info.restore();
sandbox.stub(console, 'error');
logger.error(new Error('Test error log'));
console.error.callCount.should.equal(1);
console.error.restore();
sandbox.stub(console, 'log');
logger.debug('Test debug log');
console.log.callCount.should.equal(1);
console.log.restore();
sandbox.stub(console, 'warn');
logger.warn('Test warn log');
console.warn.callCount.should.equal(1);
console.warn.restore();
});
it('will log without formatting', function() {
var logger = new Logger({formatting: false});
sandbox.stub(console, 'info');
logger.info('Test info log');
console.info.callCount.should.equal(1);
should.not.exist(console.info.args[0][0].match(/^\[/));
console.info.restore();
sandbox.stub(console, 'error');
logger.error(new Error('Test error log'));
console.error.callCount.should.equal(1);
console.error.args[0][0].should.be.instanceof(Error);
console.error.restore();
sandbox.stub(console, 'log');
logger.debug('Test debug log');
console.log.callCount.should.equal(1);
should.equal(console.log.args[0][0].match(/^\[/), null);
console.log.restore();
sandbox.stub(console, 'warn');
logger.warn('Test warn log');
console.warn.callCount.should.equal(1);
should.equal(console.warn.args[0][0].match(/^\[/), null);
console.warn.restore();
});
});