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:
parent
ea792b692f
commit
24d1bc82e9
|
@ -1,14 +1,22 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var bitcore = require('bitcore-lib');
|
||||||
|
var _ = bitcore.deps._;
|
||||||
var colors = require('colors/safe');
|
var colors = require('colors/safe');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps console.log with some special magic
|
* Wraps console.log with some special magic
|
||||||
* @constructor
|
* @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
|
* Prints an info message
|
||||||
* #info
|
* #info
|
||||||
|
@ -46,16 +54,17 @@ Logger.prototype.warn = function() {
|
||||||
* #_log
|
* #_log
|
||||||
*/
|
*/
|
||||||
Logger.prototype._log = function(color) {
|
Logger.prototype._log = function(color) {
|
||||||
if (process.env.NODE_ENV === 'test') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
args = args.slice(1);
|
args = args.slice(1);
|
||||||
var date = new Date();
|
var level = args.shift();
|
||||||
var typeString = colors[color].italic(args.shift() + ':');
|
|
||||||
args[0] = '[' + date.toISOString() + ']' + ' ' + typeString + ' ' + args[0];
|
if (this.formatting) {
|
||||||
console.log.apply(console, args);
|
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;
|
module.exports = Logger;
|
||||||
|
|
|
@ -27,6 +27,7 @@ var errors = require('./errors');
|
||||||
* ```
|
* ```
|
||||||
*
|
*
|
||||||
* @param {Object} config - The configuration of the node
|
* @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 {Array} config.services - The array of services
|
||||||
* @param {Number} config.port - The HTTP port for services
|
* @param {Number} config.port - The HTTP port for services
|
||||||
* @param {Boolean} config.https - Enable https
|
* @param {Boolean} config.https - Enable https
|
||||||
|
@ -43,6 +44,11 @@ function Node(config) {
|
||||||
this.configPath = config.path;
|
this.configPath = config.path;
|
||||||
this.errors = errors;
|
this.errors = errors;
|
||||||
this.log = log;
|
this.log = log;
|
||||||
|
|
||||||
|
if (!_.isUndefined(config.formatLogs)) {
|
||||||
|
this.log.formatting = config.formatLogs ? true : false;
|
||||||
|
}
|
||||||
|
|
||||||
this.network = null;
|
this.network = null;
|
||||||
this.services = {};
|
this.services = {};
|
||||||
this._unloadedServices = [];
|
this._unloadedServices = [];
|
||||||
|
|
|
@ -32,10 +32,10 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"preinstall": "./scripts/download",
|
"preinstall": "./scripts/download",
|
||||||
"verify": "./scripts/download --skip-bitcoin-download --verify-bitcoin-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",
|
"regtest": "./scripts/regtest",
|
||||||
"jshint": "jshint --reporter=node_modules/jshint-stylish ./lib",
|
"jshint": "jshint --reporter=node_modules/jshint-stylish ./lib",
|
||||||
"coverage": "NODE_ENV=test istanbul cover _mocha -- --recursive"
|
"coverage": "istanbul cover _mocha -- --recursive"
|
||||||
},
|
},
|
||||||
"tags": [
|
"tags": [
|
||||||
"bitcoin",
|
"bitcoin",
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue