From f1c0767ca305e7fad7bcbe6c91c7b9bf08b51aa5 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 17 Jun 2013 16:01:03 +1000 Subject: [PATCH] improved coverage --- lib/log4js.js | 8 ++--- test/logging-test.js | 73 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index d632524..e8b94ac 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -152,11 +152,11 @@ function configureAppenders(appenderList, options) { loadAppender(appenderConfig.type); var appender; appenderConfig.makers = appenderMakers; - appender = appenderMakers[appenderConfig.type](appenderConfig, options); - if (appender) { + try { + appender = appenderMakers[appenderConfig.type](appenderConfig, options); addAppender(appender, appenderConfig.category); - } else { - throw new Error("log4js configuration problem for "+util.inspect(appenderConfig)); + } catch(e) { + throw new Error("log4js configuration problem for " + util.inspect(appenderConfig), e); } }); } diff --git a/test/logging-test.js b/test/logging-test.js index c6edf7e..1026ce9 100644 --- a/test/logging-test.js +++ b/test/logging-test.js @@ -127,6 +127,42 @@ vows.describe('log4js').addBatch({ } }, + 'configuration that causes an error': { + topic: function() { + var log4js = sandbox.require( + '../lib/log4js', + { + requires: { + './appenders/file': + { + name: "file", + appender: function() {}, + configure: function(configuration) { + throw new Error("oh noes"); + } + } + } + } + ), + config = { appenders: + [ { "type" : "file", + "filename" : "cheesy-wotsits.log", + "maxLogSize" : 1024, + "backups" : 3 + } + ] + }; + try { + log4js.configure(config); + } catch (e) { + return e; + } + }, + 'should wrap error in a meaningful message': function(e) { + assert.ok(e.message.indexOf('log4js configuration problem for') > -1); + } + }, + 'configuration when passed as filename': { topic: function() { var appenderConfig, @@ -405,8 +441,11 @@ vows.describe('log4js').addBatch({ assert.instanceOf(err, Error); assert.equal(err.message, "this should not be called."); } - }, - 'configuration': { + } + }, + 'console configuration': { + topic: setupConsoleTest, + 'when disabled': { topic: function(test) { test.log4js.replaceConsole(); test.log4js.configure({ replaceConsole: false }); @@ -420,6 +459,25 @@ vows.describe('log4js').addBatch({ assert.instanceOf(err, Error); assert.equal(err.message, 'this should not be called.'); } + }, + 'when enabled': { + topic: function(test) { + test.log4js.restoreConsole(); + test.log4js.configure({ replaceConsole: true }); + //log4js.configure clears all appenders + test.log4js.addAppender(function(evt) { + test.logEvents.push(evt); + }); + + test.fakeConsole.debug("Some debug"); + return test.logEvents; + }, + + 'should allow for turning on console replacement': function (logEvents) { + assert.equal(logEvents.length, 1); + assert.equal(logEvents[0].level.toString(), "DEBUG"); + assert.equal(logEvents[0].data[0], "Some debug"); + } } }, 'configuration persistence' : { @@ -599,5 +657,16 @@ vows.describe('log4js').addBatch({ assert.equal(logEvents[0].data[0], 'info1'); assert.equal(logEvents[1].data[0], 'info3'); } + }, + + 'getDefaultLogger': { + topic: function() { + return require('../lib/log4js').getDefaultLogger(); + }, + 'should return a logger': function(logger) { + assert.ok(logger.info); + assert.ok(logger.debug); + assert.ok(logger.error); + } } }).export(module);