From 29b02921b63296deeb53775fb9ed5ab6bc118af1 Mon Sep 17 00:00:00 2001 From: Issac Goldstand Date: Thu, 2 May 2013 14:56:33 +0300 Subject: [PATCH 1/2] add option alwaysIncludePattern to dateTime appender to always use the filename with the pattern included when logging --- lib/appenders/dateFile.js | 10 +++++--- lib/streams/DateRollingFileStream.js | 10 ++++++-- test/dateFileAppender-test.js | 38 +++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 6 deletions(-) diff --git a/lib/appenders/dateFile.js b/lib/appenders/dateFile.js index e445516..ec75f90 100644 --- a/lib/appenders/dateFile.js +++ b/lib/appenders/dateFile.js @@ -19,10 +19,10 @@ process.on('exit', function() { * also used to check when to roll files - defaults to '.yyyy-MM-dd' * @layout layout function for log messages - defaults to basicLayout */ -function appender(filename, pattern, layout) { +function appender(filename, pattern, alwaysIncludePattern, layout) { layout = layout || layouts.basicLayout; - var logFile = new streams.DateRollingFileStream(filename, pattern); + var logFile = new streams.DateRollingFileStream(filename, pattern, alwaysIncludePattern); openFiles.push(logFile); return function(logEvent) { @@ -37,12 +37,16 @@ function configure(config, options) { if (config.layout) { layout = layouts.layout(config.layout.type, config.layout); } + + if (!config.alwaysIncludePattern) { + config.alwaysIncludePattern = false; + } if (options && options.cwd && !config.absolute) { config.filename = path.join(options.cwd, config.filename); } - return appender(config.filename, config.pattern, layout); + return appender(config.filename, config.pattern, config.alwaysIncludePattern, layout); } exports.appender = appender; diff --git a/lib/streams/DateRollingFileStream.js b/lib/streams/DateRollingFileStream.js index 9044b91..01755f6 100644 --- a/lib/streams/DateRollingFileStream.js +++ b/lib/streams/DateRollingFileStream.js @@ -13,7 +13,7 @@ if (process.env.NODE_DEBUG && /\blog4js\b/.test(process.env.NODE_DEBUG)) { debug = function() { }; } -function DateRollingFileStream(filename, pattern, options, now) { +function DateRollingFileStream(filename, pattern, alwaysIncludePattern, options, now) { debug("Now is " + now); if (pattern && typeof(pattern) === 'object') { now = options; @@ -23,6 +23,12 @@ function DateRollingFileStream(filename, pattern, options, now) { this.pattern = pattern || '.yyyy-MM-dd'; this.now = now || Date.now; this.lastTimeWeWroteSomething = format.asString(this.pattern, new Date(this.now())); + + this.alwaysIncludePattern = alwaysIncludePattern; + if (this.alwaysIncludePattern) { + this.baseFilename = filename; + filename = filename + this.lastTimeWeWroteSomething; + } debug("this.now is " + this.now + ", now is " + now); DateRollingFileStream.super_.call(this, filename, options); @@ -43,7 +49,7 @@ DateRollingFileStream.prototype.shouldRoll = function() { DateRollingFileStream.prototype.roll = function(filename, callback) { var that = this, - newFilename = filename + this.previousTime; + newFilename = this.baseFilename + this.previousTime; debug("Starting roll"); diff --git a/test/dateFileAppender-test.js b/test/dateFileAppender-test.js index 49a1ce3..9f28bcf 100644 --- a/test/dateFileAppender-test.js +++ b/test/dateFileAppender-test.js @@ -92,7 +92,43 @@ vows.describe('../lib/appenders/dateFile').addBatch({ assert.include(contents, 'this should be written to the file\n'); assert.equal(contents.indexOf('this should not be written to the file'), -1); } - } + }, + 'with options.alwaysIncludePattern': { + topic: function() { + var log4js = require('../lib/log4js') + , format = require('../lib/date_format') + , logger + , options = { + "appenders": [ + { + "category": "tests", + "type": "dateFile", + "filename": "test/date-file-test", + "pattern": "-from-MM-dd.log", + "alwaysIncludePattern": true, + "layout": { + "type": "messagePassThrough" + } + } + ] + } + , thisTime = format.asString(options.appenders[0].pattern, new Date()); + log4js.clearAppenders(); + log4js.configure(options); + logger = log4js.getLogger('tests'); + logger.warn('this should be written to the file'); + return thisTime; + }, + teardown: function(topic) { + removeFile('date-file-test' + topic); + }, + 'should create file with the correct pattern': function(topic) { + assert.equal(fs.existsSync(path.join(__dirname, 'date-file-test' + topic)), true); + }, + 'should not create file with the base filename': function(topic) { + assert.equal(fs.existsSync(path.join(__dirname, 'date-file-test')), false); + } + } } }).exportTo(module); From 04de4ed8d32e6ba1b296036c22d63516312d6d46 Mon Sep 17 00:00:00 2001 From: Issac Goldstand Date: Fri, 3 May 2013 11:14:28 +0300 Subject: [PATCH 2/2] fix OS-specific endline mucking test results (:-O not everyone uses linux?!?!) --- test/dateFileAppender-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/dateFileAppender-test.js b/test/dateFileAppender-test.js index 9f28bcf..50a0458 100644 --- a/test/dateFileAppender-test.js +++ b/test/dateFileAppender-test.js @@ -89,7 +89,7 @@ vows.describe('../lib/appenders/dateFile').addBatch({ teardown: removeFile('date-file-test.log'), 'should load appender configuration from a json file': function(err, contents) { - assert.include(contents, 'this should be written to the file\n'); + assert.include(contents, 'this should be written to the file' + require('os').EOL); assert.equal(contents.indexOf('this should not be written to the file'), -1); } },