changed fileappender to use writeStream instead of fs.write, tests don't work

This commit is contained in:
Gareth Jones 2011-07-17 12:28:26 +10:00
parent d64d4ca0ca
commit 3d27140a9d
2 changed files with 48 additions and 7 deletions

View File

@ -283,16 +283,20 @@ function consoleAppender (layout) {
*/ */
function fileAppender (file, layout, logSize, numBackups, filePollInterval) { function fileAppender (file, layout, logSize, numBackups, filePollInterval) {
layout = layout || layouts.basicLayout; layout = layout || layouts.basicLayout;
//syncs are generally bad, but we need var logFile = fs.createWriteStream(file, { flags: 'a', mode: 0644, encoding: 'utf8' });
//the file to be open before we start doing any writing.
var logFile = fs.openSync(file, 'a', 0644);
if (logSize > 0) { if (logSize > 0) {
setupLogRolling(logFile, file, logSize, numBackups || 5, (filePollInterval * 1000) || 30000); 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) { 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) { 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. //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) //roll the backups (rename file.n-1 to file.n, where n <= numBackups)
for (var i=numBackups; i > 0; i--) { for (var i=numBackups; i > 0; i--) {
if (i > 1) { if (i > 1) {
@ -326,7 +330,7 @@ function rollThatLog (logFile, filename, numBackups) {
} }
} }
//open it up again //open it up again
logFile = fs.openSync(filename, 'a', 0644); logFile = fs.createWriteStream(filename, { flags: 'a', mode: 0644, encoding: "utf8" });
} }
function fileExists (filename) { function fileExists (filename) {

37
memory-test.js Normal file
View File

@ -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.");
}