From 8d542f1aa612ca7939c2fadeec03c74632e95619 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 11 Aug 2010 13:36:51 +1000 Subject: [PATCH 1/4] Fixed tests to work with node-v0.1.102 --- spec/spec.logging.js | 29 +++++++++++++++++++---------- tests.js | 6 ++---- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/spec/spec.logging.js b/spec/spec.logging.js index 312b8e0..17fba5b 100644 --- a/spec/spec.logging.js +++ b/spec/spec.logging.js @@ -1,11 +1,15 @@ -fs = require('fs'), events = require('events'); - -waitForWriteAndThenReadFile = function (filename) { - process.loop(); - return fs.readFileSync(filename); -}; - describe 'log4js' + before + extend(context, { + log4js : require("log4js"), + fs: require("fs"), + waitForWriteAndThenReadFile : function (filename) { + process.loop(); + return fs.readFileSync(filename, "utf8"); + } + }); + end + before_each log4js.clearAppenders(); event = ''; @@ -132,7 +136,7 @@ describe 'log4js' try { fs.unlinkSync('./tmp-tests.log'); } catch(e) { - print('Could not delete tmp-tests.log: '+e.message); + //print('Could not delete tmp-tests.log: '+e.message); } end @@ -171,12 +175,12 @@ describe 'log4js' try { fs.unlinkSync('./tmp-tests.log'); } catch(e) { - print('Could not delete tmp-tests.log: '+e.message); + //print('Could not delete tmp-tests.log: '+e.message); } try { fs.unlinkSync('./tmp-tests-warnings.log'); } catch (e) { - print('Could not delete tmp-tests-warnings.log: '+e.message); + //print('Could not delete tmp-tests-warnings.log: '+e.message); } end @@ -207,9 +211,14 @@ describe 'log4js' waitForWriteAndThenReadFile('./tmp-tests-warnings.log').should.be 'both\nboth\n' end end + end describe 'Date' + before + require("log4js"); + end + describe 'toFormattedString' it 'should add a toFormattedString method to Date' var date = new Date(); diff --git a/tests.js b/tests.js index 3af4f2d..3090813 100644 --- a/tests.js +++ b/tests.js @@ -1,8 +1,6 @@ require.paths.unshift("./spec/lib", "./lib"); require("jspec"); -log4js = require("log4js-node"); - var sys = require("sys"), fs = require("fs"); quit = process.exit @@ -11,7 +9,7 @@ print = sys.puts readFile = function(path) { var result; try { - result = fs.readFileSync(path); + result = fs.readFileSync(path, "utf8"); } catch (e) { throw e; } @@ -37,7 +35,7 @@ if (process.ARGV[2]) { ); } if (specsFound) { - JSpec.run({ reporter: JSpec.reporters.Terminal }); + JSpec.run({ reporter: JSpec.reporters.Terminal, failuresOnly: false }); JSpec.report(); } else { print("No tests to run. This makes me sad."); From 17c98947cbb79ec7aaa9e3d4252aace8c2bb48a0 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 11 Aug 2010 13:39:33 +1000 Subject: [PATCH 2/4] Fixed readme example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 95115fe..981d52c 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Run the tests with `node tests.js`. They use the awesome [jspec](http://visionme See example.js: - var log4js = require('log4js-node'); + var log4js = require('log4js'); log4js.addAppender(log4js.consoleAppender()); log4js.addAppender(log4js.fileAppender('logs/cheese.log'), 'cheese'); From caa7338b046e1bf132f82f8f03b1bdf23942f9bb Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 11 Aug 2010 13:46:59 +1000 Subject: [PATCH 3/4] fixed example --- example.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.js b/example.js index b15fdfb..b4eab22 100644 --- a/example.js +++ b/example.js @@ -1,4 +1,4 @@ -var log4js = require('./lib/log4js-node'); +var log4js = require('./lib/log4js'); log4js.addAppender(log4js.consoleAppender()); log4js.addAppender(log4js.fileAppender('cheese.log'), 'cheese'); From 5ffc00c9dea9df15793ea141a4fe8b201b25326a Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 7 Oct 2010 15:15:23 +1100 Subject: [PATCH 4/4] appenders can be added to multiple categories at the same time --- lib/log4js.js | 22 +++++++++++++++---- package.json | 2 +- spec/lib/jspec.js | 2 +- spec/spec.logging.js | 52 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index 6857baf..e5d73d4 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -55,7 +55,7 @@ var log4js = { * @static * @final */ - version: "0.1.0", + version: "0.1.1", /** * Date of logger initialized. @@ -119,8 +119,21 @@ exports.getDefaultLogger = log4js.getDefaultLogger = function() { return log4js.getLogger(DEFAULT_CATEGORY); }; -exports.addAppender = log4js.addAppender = function (appender, categoryName) { - var category = categoryName || ALL_CATEGORIES; +/** + * args are appender, then zero or more categories + */ +exports.addAppender = log4js.addAppender = function () { + var args = Array.prototype.slice.call(arguments); + var appender = args.shift(); + if (args.length == 0) { + args = [ ALL_CATEGORIES ]; + } + //argument may already be an array + if (args[0].forEach) { + args = args[0]; + } + + args.forEach(function(category) { if (!log4js.appenders[category]) { log4js.appenders[category] = []; } @@ -135,13 +148,14 @@ exports.addAppender = log4js.addAppender = function (appender, categoryName) { } else if (log4js.loggers[category]) { log4js.loggers[category].addListener("log", appender); } + }); }; exports.clearAppenders = log4js.clearAppenders = function() { log4js.appenders = []; for (var logger in log4js.loggers) { if (log4js.loggers.hasOwnProperty(logger)) { - log4js.loggers[logger].listeners.length = 0; + log4js.loggers[logger].removeAllListeners("log"); } } }; diff --git a/package.json b/package.json index 9058c2f..16f1429 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "log4js", - "version": "0.1.0", + "version": "0.1.1", "description": "Port of Log4js to work with node.", "keywords": [ "logging", diff --git a/spec/lib/jspec.js b/spec/lib/jspec.js index 1e95f93..742987c 100644 --- a/spec/lib/jspec.js +++ b/spec/lib/jspec.js @@ -1770,4 +1770,4 @@ } }) -})() \ No newline at end of file +})() diff --git a/spec/spec.logging.js b/spec/spec.logging.js index 17fba5b..fbaea45 100644 --- a/spec/spec.logging.js +++ b/spec/spec.logging.js @@ -48,7 +48,6 @@ describe 'log4js' describe 'addAppender' before_each - log4js.clearAppenders(); appenderEvent = undefined; appender = function(logEvent) { appenderEvent = logEvent; }; end @@ -72,6 +71,15 @@ describe 'log4js' appenderEvent.should.be event otherEvent.should.be event + + otherEvent = undefined; + appenderEvent = undefined; + log4js.getLogger('pants').debug("this should not be propagated to otherEvent"); + otherEvent.should.be undefined + appenderEvent.should.not.be undefined + appenderEvent.message.should.be "this should not be propagated to otherEvent" + + cheeseLogger = null; end end @@ -88,6 +96,45 @@ describe 'log4js' end end + describe 'with multiple categories' + it 'should register the function as a listener for all the categories' + log4js.addAppender(appender, 'tests', 'biscuits'); + + logger.debug('this is a test'); + appenderEvent.should.be event + appenderEvent = undefined; + + var otherLogger = log4js.getLogger('biscuits'); + otherLogger.debug("mmm... garibaldis"); + appenderEvent.should.not.be undefined + appenderEvent.message.should.be "mmm... garibaldis" + appenderEvent = undefined; + + otherLogger = null; + + log4js.getLogger("something else").debug("pants"); + appenderEvent.should.be undefined + end + + it 'should register the function when the list of categories is an array' + log4js.addAppender(appender, ['tests', 'pants']); + + logger.debug('this is a test'); + appenderEvent.should.be event + appenderEvent = undefined; + + var otherLogger = log4js.getLogger('pants'); + otherLogger.debug("big pants"); + appenderEvent.should.not.be undefined + appenderEvent.message.should.be "big pants" + appenderEvent = undefined; + + otherLogger = null; + + log4js.getLogger("something else").debug("pants"); + appenderEvent.should.be undefined + end + end end describe 'basicLayout' @@ -189,6 +236,8 @@ describe 'log4js' //and sets the log level for "tests" to WARN log4js.configure('spec/fixtures/log4js.json'); event = undefined; + logger = log4js.getLogger("tests"); + logger.addListener("log", function(evt) { event = evt }); logger.info('this should not fire an event'); event.should.be undefined @@ -200,7 +249,6 @@ describe 'log4js' it 'should handle logLevelFilter configuration' log4js.configure('spec/fixtures/with-logLevelFilter.json'); - event = undefined; logger.info('main'); logger.error('both');