From 3d27140a9d1b5ee4eac8ccd282b09f5eeedcba4d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 17 Jul 2011 12:28:26 +1000 Subject: [PATCH] changed fileappender to use writeStream instead of fs.write, tests don't work --- lib/log4js.js | 18 +++++++++++------- memory-test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 memory-test.js diff --git a/lib/log4js.js b/lib/log4js.js index 7b40b29..d89d7a4 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -283,16 +283,20 @@ function consoleAppender (layout) { */ function fileAppender (file, layout, logSize, numBackups, filePollInterval) { layout = layout || layouts.basicLayout; - //syncs are generally bad, but we need - //the file to be open before we start doing any writing. - var logFile = fs.openSync(file, 'a', 0644); + var logFile = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' }); if (logSize > 0) { setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000); } + //close the file on process exit, otherwise the process won't die. + process.on('exit', function() { + logFile.end(); + logFile.destroySoon(); + }); + return function(loggingEvent) { - fs.write(logFile, layout(loggingEvent)+'\n', null, "utf8"); + logFile.write(layout(loggingEvent)+'\n'); }; } @@ -312,9 +316,9 @@ function setupLogRolling (logFile, filename, logSize, numBackups, filePollInterv } function rollThatLog (logFile, filename, numBackups) { - //doing all of this fs stuff sync, because I don't want to lose any log events. //first close the current one. - fs.closeSync(logFile); + logFile.end(); + logFile.destroySoon(); //roll the backups (rename file.n-1 to file.n, where n <= numBackups) for (var i=numBackups; i > 0; i--) { if (i > 1) { @@ -326,7 +330,7 @@ function rollThatLog (logFile, filename, numBackups) { } } //open it up again - logFile = fs.openSync(filename, 'a', 0644); + logFile = fs.createWriteStream(filename, { flags: 'a', mode: 0644, encoding: "utf8" }); } function fileExists (filename) { diff --git a/memory-test.js b/memory-test.js new file mode 100644 index 0000000..ac2ae04 --- /dev/null +++ b/memory-test.js @@ -0,0 +1,37 @@ +var log4js = require('./lib/log4js') +, logger +, usage +, i; + +log4js.configure( + { + appenders: [ + { + category: "memory-test" + , type: "file" + , filename: "memory-test.log" + }, + { + type: "console" + , category: "memory-usage" + }, + { + type: "file" + , filename: "memory-usage.log" + , category: "memory-usage" + , layout: { + type: "messagePassThrough" + } + } + ] + } +); +logger = log4js.getLogger("memory-test"); +usage = log4js.getLogger("memory-usage"); + +for (i=0; i < 1000000; i++) { + if ( (i % 5000) === 0) { + usage.info("%d %d", i, process.memoryUsage().rss); + } + logger.info("Doing something."); +}