From 53367785b4371b981f224a4223d9369084e7d269 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Wed, 22 Feb 2012 08:53:28 +1100 Subject: [PATCH] got rid of the __preLog4js stuff from the console.log replacement --- lib/log4js.js | 28 +++++++++++++++------------- lib/streams.js | 14 +++++++------- test/fileAppender.js | 5 ++++- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/lib/log4js.js b/lib/log4js.js index 249a1f1..fbf929c 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -357,28 +357,30 @@ function configure (configurationFileOrObject, options) { configureOnceOff(config); } +var originalConsoleFunctions = { + log: console.log, + debug: console.debug, + info: console.info, + warn: console.warn, + error: console.error +}; + function replaceConsole(logger) { function replaceWith(fn) { return function() { fn.apply(logger, arguments); } } - if (console['_preLog4js_log'] === undefined) { - logger = logger || getLogger("console"); - ['log','debug','info','warn','error'].forEach(function (item) { - console['_preLog4js_'+item] = console[item]; - console[item] = replaceWith(item === 'log' ? logger.info : logger[item]); - }); - } + logger = logger || getLogger("console"); + ['log','debug','info','warn','error'].forEach(function (item) { + console[item] = replaceWith(item === 'log' ? logger.info : logger[item]); + }); } function restoreConsole() { - if (console['_preLog4js_log']) { - ['log', 'debug', 'info', 'warn', 'error'].forEach(function (item) { - console[item] = console['_preLog4js_'+item]; - delete console['_preLog4js_'+item]; - }); - } + ['log', 'debug', 'info', 'warn', 'error'].forEach(function (item) { + console[item] = originalConsoleFunctions[item]; + }); } function loadAppender(appender) { diff --git a/lib/streams.js b/lib/streams.js index 8ead2b6..ab1a362 100644 --- a/lib/streams.js +++ b/lib/streams.js @@ -1,12 +1,12 @@ -var util = require('util'), -fs = require('fs'), -path = require('path'), -events = require('events'), +var util = require('util'), +fs = require('fs'), +path = require('path'), +events = require('events'), async = require('async'); function debug(message) { -// util.debug(message); - console.log(message); + util.debug(message); +// console.log(message); } function BufferedWriteStream(stream) { @@ -265,4 +265,4 @@ RollingFileStream.prototype.roll = function(filename, callback) { exports.BaseRollingFileStream = BaseRollingFileStream; exports.RollingFileStream = RollingFileStream; -exports.BufferedWriteStream = BufferedWriteStream; \ No newline at end of file +exports.BufferedWriteStream = BufferedWriteStream; diff --git a/test/fileAppender.js b/test/fileAppender.js index 567ff82..ab8f3d3 100644 --- a/test/fileAppender.js +++ b/test/fileAppender.js @@ -20,6 +20,7 @@ vows.describe('log4js fileAppender').addBatch({ var that = this, testFile = __dirname + '/fa-default-test.log' , logger = log4js.getLogger('default-settings'); remove(testFile); + log4js.clearAppenders(); log4js.addAppender(log4js.fileAppender(testFile), 'default-settings'); logger.info("This should be in the file."); @@ -43,6 +44,7 @@ vows.describe('log4js fileAppender').addBatch({ remove(testFile); remove(testFile + '.1'); //log file of 100 bytes maximum, no backups + log4js.clearAppenders(); log4js.addAppender(log4js.fileAppender(testFile, log4js.layouts.basicLayout, 100, 0), 'max-file-size'); logger.info("This is the first log message."); logger.info("This is an intermediate log message."); @@ -76,6 +78,7 @@ vows.describe('log4js fileAppender').addBatch({ remove(testFile+'.2'); //log file of 50 bytes maximum, 2 backups + log4js.clearAppenders(); log4js.addAppender(log4js.fileAppender(testFile, log4js.layouts.basicLayout, 50, 2), 'max-file-size-backups'); logger.info("This is the first log message."); logger.info("This is the second log message."); @@ -146,4 +149,4 @@ vows.describe('log4js fileAppender').addBatch({ } } -}).export(module); \ No newline at end of file +}).export(module);