From 24d1bc82e9e3ff9ab49f5b292624fddd5773cf54 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 27 Apr 2016 13:46:50 -0400 Subject: [PATCH] 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. --- lib/logger.js | 27 ++++++++++----- lib/node.js | 6 ++++ package.json | 4 +-- test/logger.unit.js | 83 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 11 deletions(-) create mode 100644 test/logger.unit.js diff --git a/lib/logger.js b/lib/logger.js index 8d9f48e0..4084c2cc 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -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; diff --git a/lib/node.js b/lib/node.js index 4d80fc48..75431598 100644 --- a/lib/node.js +++ b/lib/node.js @@ -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 = []; diff --git a/package.json b/package.json index a2c388ce..2c61bebc 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/logger.unit.js b/test/logger.unit.js new file mode 100644 index 00000000..75522814 --- /dev/null +++ b/test/logger.unit.js @@ -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(); + }); + +});