From a876dfbe9cc5e54fde2a96234a3788ca712b3b1f Mon Sep 17 00:00:00 2001 From: csausdev Date: Sun, 16 Jan 2011 13:21:37 +1100 Subject: [PATCH] configure now takes a filename or object --- lib/log4js.js | 19 +++++++++++-------- test/logging.js | 20 +++++++++++++++++++- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index 4a2dbfd..460161c 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -591,16 +591,19 @@ module.exports = function (fileSystem, standardOutput, configPaths) { } } - function configure (configurationFile) { - if (configurationFile) { - try { - var config = JSON.parse(fs.readFileSync(configurationFile, "utf8")); - configureAppenders(config.appenders, fileAppender, consoleAppender); - configureLevels(config.levels); + function configure (configurationFileOrObject) { + var config = configurationFileOrObject; + if (typeof(config) === "string") { + config = JSON.parse(fs.readFileSync(config, "utf8")); + } + if (config) { + try { + configureAppenders(config.appenders, fileAppender, consoleAppender); + configureLevels(config.levels); } catch (e) { - throw new Error("Problem reading log4js config file " + configurationFile + ". Error was \"" + e.message + "\" ("+e.stack+")"); + throw new Error("Problem reading log4js config " + sys.inspect(config) + ". Error was \"" + e.message + "\" ("+e.stack+")"); } - } + } } function findConfiguration() { diff --git a/test/logging.js b/test/logging.js index b108ec1..42b71a6 100644 --- a/test/logging.js +++ b/test/logging.js @@ -242,7 +242,25 @@ vows.describe('log4js').addBatch({ delete messages['tmp-test.log']; log4js.configure('test/with-log-rolling.json'); assert.equal(messages.watchedFile, 'tmp-test.log'); - } + }, + 'should handle an object or a file name': function(args) { + var log4js = args[0], + messages = args[1], + config = { + "appenders": [ + { + "type" : "file", + "filename" : "cheesy-wotsits.log", + "maxLogSize" : 1024, + "backups" : 3, + "pollInterval" : 15 + } + ] + }; + delete messages['cheesy-wotsits.log']; + log4js.configure(config); + assert.equal(messages.watchedFile, 'cheesy-wotsits.log'); + } }, 'with no appenders defined' : {