8000 fixed to use streamroller · mKoder/log4js-node@7f07816 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f07816

Browse files
author
Gareth Jones
committed
fixed to use streamroller
1 parent 2162b0e commit 7f07816

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

lib/appenders/file.js

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"use strict";
2-
var layouts = require('../layouts')
2+
var debug = require('debug')('log4js:file')
3+
, layouts = require('../layouts')
34
, path = require('path')
45
, fs = require('fs')
56
, streams = require('streamroller')
@@ -34,24 +35,25 @@ process.on('SIGHUP', function() {
3435
* if not provided then logs won't be rotated.
3536
* @param numBackups - the number of log files to keep after logSize
3637
* has been reached (default 5)
37-
* @param compress - flag that controls log file compression
38+
* @param options - options to be passed to the underlying stream
3839
* @param timezoneOffset - optional timezone offset in minutes (default system local)
3940
*/
40-
function fileAppender (file, layout, logSize, numBackups, compress, timezoneOffset) {
41+
function fileAppender (file, layout, logSize, numBackups, options, timezoneOffset) {
4142
var bytesWritten = 0;
4243
file = path.normalize(file);
4344
layout = layout || layouts.basicLayout;
4445
numBackups = numBackups === undefined ? 5 : numBackups;
4546
//there has to be at least one backup if logSize has been specified
4647
numBackups = numBackups === 0 ? 1 : numBackups;
4748

49+
debug("Creating file appender (", file, ", ", logSize, ", ", numBackups, ", ", options, ")");
4850
var writer = {
49-
stream: openTheStream(file, logSize, numBackups),
51+
stream: openTheStream(file, logSize, numBackups, options),
5052
reopen: function() {
5153
this.stream.end();
52-
this.stream = openTheStream(file, logSize, numBackups);
54+
this.stream = openTheStream(file, logSize, numBackups, options);
5355
}
54-
}
56+
};
5557

5658
// push file to the stack of open handlers
5759
openFiles.push(writer);
@@ -62,23 +64,13 @@ function fileAppender (file, layout, logSize, numBackups, compress, timezoneOffs
6264

6365
}
6466

65-
function openTheStream(file, fileSize, numFiles) {
66-
var stream;
67-
if (fileSize) {
68-
stream = new streams.RollingFileStream(
69-
file,
70-
fileSize,
71-
numFiles,
72-
{ "compress": compress }
73-
);
74-
} else {
75-
stream = fs.createWriteStream(
76-
file,
77-
{ encoding: "utf8",
78-
mode: parseInt('0644', 8),
79-
flags: 'a' }
80-
);
81-
}
67+
function openTheStream(file, fileSize, numFiles, options) {
68+
var stream = new streams.RollingFileStream(
69+
file,
70+
fileSize,
71+
numFiles,
72+
options
73+
);
8274
stream.on("error", function (err) {
8375
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
8476
});
@@ -101,8 +93,8 @@ function configure(config, options) {
10193
layout,
10294
config.maxLogSize,
10395
config.backups,
104-
config.compress,
105-
config.timezoneOffset
96+
config.timezoneOffset,
97+
config
10698
);
10799
}
108100

test/vows/fileAppender-test.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ vows.describe('log4js fileAppender').addBatch({
2626
, count = 5, logfile;
2727

2828
while (count--) {
29-
logfile = path.join(__dirname, '/fa-default-test' + count + '.log');
29+
logfile = path.join(__dirname, 'fa-default-test' + count + '.log');
3030
log4js.addAppender(
3131
require('../../lib/appenders/file').appender(logfile),
3232
'default-settings'
@@ -36,8 +36,8 @@ vows.describe('log4js fileAppender').addBatch({
3636
return listenersCount;
3737
},
3838

39-
'does not add more than one `exit` listeners': function (initialCount) {
40-
assert.ok(process.listeners('exit').length <= initialCount + 1);
39+
'does not add more than one `exit` listener': function (initialCount) {
40+
assert.equal(initialCount + 1, process.listeners('exit').length);
4141
}
4242
},
4343

@@ -51,7 +51,9 @@ vows.describe('log4js fileAppender').addBatch({
5151
globals: {
5252
process: {
5353
on: function(evt, listener) {
54-
exitListener = listener;
54+
if (evt == 'exit') {
55+
exitListener = listener;
56+
}
5557
}
5658
}
5759
},
@@ -196,7 +198,7 @@ vows.describe('log4js fileAppender').addBatch({
196198
},
197199
'with a max file size and no backups': {
198200
topic: function() {
199-
var testFile = path.join(__dirname, '/fa-maxFileSize-test.log')
201+
var testFile = path.join(__dirname, 'fa-maxFileSize-test.log')
200202
, logger = log4js.getLogger('max-file-size')
201203
, that = this;
202204
remove(testFile);
@@ -234,7 +236,7 @@ vows.describe('log4js fileAppender').addBatch({
234236
},
235237
'with a max file size and 2 backups': {
236238
topic: function() {
237-
var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-test.log')
239+
var testFile = path.join(__dirname, 'fa-maxFileSize-with-backups-test.log')
238240
, logger = log4js.getLogger('max-file-size-backups');
239241
remove(testFile);
240242
remove(testFile+'.1');
@@ -307,7 +309,7 @@ vows.describe('log4js fileAppender').addBatch({
307309
},
308310
'with a max file size and 2 compressed backups': {
309311
topic: function() {
310-
var testFile = path.join(__dirname, '/fa-maxFileSize-with-backups-compressed-test.log')
312+
var testFile = path.join(__dirname, 'fa-maxFileSize-with-backups-compressed-test.log')
311313
, logger = log4js.getLogger('max-file-size-backups');
312314
remove(testFile);
313315
remove(testFile+'.1.gz');
@@ -317,7 +319,7 @@ vows.describe('log4js fileAppender').addBatch({
317319
log4js.clearAppenders();
318320
log4js.addAppender(
319321
require('../../lib/appenders/file').appender(
320-
testFile, log4js.layouts.basicLayout, 50, 2, true
322+
testFile, log4js.layouts.basicLayout, 50, 2, { compress: true }
321323
),
322324
'max-file-size-backups'
323325
);

0 commit comments

Comments
 (0)
0