8000 Fixes for version v0.10 streams, breaks log4js for older versions of … · rboss/log4js-node@65e490c · GitHub
[go: up one dir, main page]

Skip to content

Commit 65e490c

Browse files
author
Gareth Jones
committed
Fixes for version v0.10 streams, breaks log4js for older versions of node
1 parent 5e242c9 commit 65e490c

17 files changed

+216
-481
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
language: node_js
22
node_js:
3-
- 0.6
4-
- 0.8
3+
- 0.10

examples/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var log4js = require('./lib/log4js');
1+
var log4js = require('../lib/log4js');
22
//log the cheese logger messages to a file, and the console ones as well.
33
log4js.configure({
44
appenders: [

examples/log-rolling.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var log4js = require('./lib/log4js')
1+
var log4js = require('../lib/log4js')
22
, log
33
, i = 0;
44
log4js.configure({

lib/appenders/dateFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ process.on('exit', function() {
2222
function appender(filename, pattern, layout) {
2323
layout = layout || layouts.basicLayout;
2424

25-
var logFile = new streams.BufferedWriteStream(new streams.DateRollingFileStream(filename, pattern));
25+
var logFile = new streams.DateRollingFileStream(filename, pattern);
2626
openFiles.push(logFile);
2727

2828
return function(logEvent) {

lib/appenders/file.js

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,51 @@ process.on('exit', function() {
2222
* @param numBackups - the number of log files to keep after logSize has been reached (default 5)
2323
*/
2424
function fileAppender (file, layout, logSize, numBackups) {
25-
var bytesWritten = 0;
26-
file = path.normalize(file);
27-
layout = layout || layouts.basicLayout;
28-
numBackups = numBackups === undefined ? 5 : numBackups;
29-
//there has to be at least one backup if logSize has been specified
30-
numBackups = numBackups === 0 ? 1 : numBackups;
25+
var bytesWritten = 0;
26+
file = path.normalize(file);
27+
layout = layout || layouts.basicLayout;
28+
numBackups = numBackups === undefined ? 5 : numBackups;
29+
//there has to be at least one backup if logSize has been specified
30+
numBackups = numBackups === 0 ? 1 : numBackups;
3131

32-
function openTheStream(file, fileSize, numFiles) {
33-
var stream;
34-
if (fileSize) {
35-
stream = new streams.BufferedWriteStream(
36-
new streams.RollingFileStream(
37-
file,
38-
fileSize,
39-
numFiles
40-
)
41-
);
42-
} else {
43-
stream = new streams.BufferedWriteStream(fs.createWriteStream(file, { encoding: "utf8", mode: 0644, flags: 'a' }));
44-
}
45-
stream.on("error", function (err) {
46-
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
47-
});
48-
return stream;
32+
function openTheStream(file, fileSize, numFiles) {
33+
var stream;
34+
if (fileSize) {
35+
stream = new streams.RollingFileStream(
36+
file,
37+
fileSize,
38+
numFiles
39+
);
40+
} else {
41+
stream = fs.createWriteStream(file, { encoding: "utf8", mode: 0644, flags: 'a' });
4942
}
43+
stream.on("error", function (err) {
44+
console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
45+
});
46+
return stream;
47+
}
5048

51-
var logFile = openTheStream(file, logSize, numBackups);
52-
53-
// push file to the stack of open handlers
54-
openFiles.push(logFile);
55-
56-
return function(loggingEvent) {
57-
logFile.write(layout(loggingEvent) + eol, "utf8");
58-
};
49+
var logFile = openTheStream(file, logSize, numBackups);
50+
51+
// push file to the stack of open handlers
52+
openFiles.push(logFile);
53+
54+
return function(loggingEvent) {
55+
logFile.write(layout(loggingEvent) + eol, "utf8");
56+
};
5957
}
6058

6159
function configure(config, options) {
62-
var layout;
63-
if (config.layout) {
64-
layout = layouts.layout(config.layout.type, config.layout);
65-
}
60+
var layout;
61+
if (config.layout) {
62+
layout = layouts.layout(config.layout.type, config.layout);
63+
}
6664

67-
if (options && options.cwd && !config.absolute) {
68-
config.filename = path.join(options.cwd, config.filename);
69-
}
65+
if (options && options.cwd && !config.absolute) {
66+
config.filename = path.join(options.cwd, config.filename);
67+
}
7068

71-
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
69+
return fileAppender(config.filename, layout, config.maxLogSize, config.backups);
7270
}
7371

7472
exports.appender = fileAppender;

lib/streams/BaseRollingFileStream.js

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var fs = require('fs'),
2-
util = require('util');
2+
stream = require('stream'),
3+
util = require('util');
34

45
function debug(message) {
56
// console.log(message);
@@ -8,85 +9,64 @@ function debug(message) {
89
module.exports = BaseRollingFileStream;
910

1011
function BaseRollingFileStream(filename, options) {
12+
debug("In BaseRollingFileStream");
13+
this.filename = filename;
14+
this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
15+
this.currentSize = 0;
1116

12-
debug("In BaseRollingFileStream");
13-
this.filename = filename;
14-
this.options = options || { encoding: 'utf8', mode: 0644, flags: 'a' };
15-
this.rolling = false;
16-
this.writesWhileRolling = [];
17-
this.currentSize = 0;
18-
this.rollBeforeWrite = false;
19-
20-
function currentFileSize(file) {
21-
var fileSize = 0;
22-
try {
23-
fileSize = fs.statSync(file).size;
24-
} catch (e) {
25-
// file does not exist
26-
}
27-
return fileSize;
17+
function currentFileSize(file) {
18+
var fileSize = 0;
19+
try {
20+
fileSize = fs.statSync(file).size;
21+
} catch (e) {
22+
// file does not exist
2823
}
24+
return fileSize;
25+
}
2926

30-
function throwErrorIfArgumentsAreNotValid() {
31-
if (!filename) {
32-
throw new Error("You must specify a filename");
33-
}
27+
function throwErrorIfArgumentsAreNotValid() {
28+
if (!filename) {
29+
throw new Error("You must specify a filename");
3430
}
31+
}
3532

36-
throwErrorIfArgumentsAreNotValid();
37-
debug("Calling BaseRollingFileStream.super");
38-
BaseRollingFileStream.super_.call(this, this.filename, this.options);
39-
this.currentSize = currentFileSize(this.filename);
33+
throwErrorIfArgumentsAreNotValid();
34+
debug("Calling BaseRollingFileStream.super");
35+
BaseRollingFileStream.super_.call(this);
36+
this.openTheStream();
37+
this.currentSize = currentFileSize(this.filename);
4038
}
41-
util.inherits(BaseRollingFileStream, fs.FileWriteStream);
39+
util.inherits(BaseRollingFileStream, stream.Writable);
4240

43-
BaseRollingFileStream.prototype.initRolling = function() {
44-
var that = this;
41+
BaseRollingFileStream.prototype._write = function(chunk, encoding, callback) {
42+
var that = this;
43+
function writeTheChunk() {
44+
debug("writing the chunk to the underlying stream");
45+
that.currentSize += chunk.length;
46+
that.theStream.write(chunk, encoding, callback);
47+
}
4548

46-
function emptyRollingQueue() {
47-
debug("emptying the rolling queue");
48-
var toWrite;
49-
while ((toWrite = that.writesWhileRolling.shift())) {
50-
BaseRollingFileStream.super_.prototype.write.call(that, toWrite.data, toWrite.encoding);
51-
that.currentSize += toWrite.data.length;
52-
if (that.shouldRoll()) {
53-
that.flush();
54-
return true;
55-
}
56-
}
57-
that.flush();
58-
return false;
59-
}
49+
debug("in _write");
6050

61-
this.rolling = true;
62-
this.roll(this.filename, function() {
63-
that.currentSize = 0;
64-
that.rolling = emptyRollingQueue();
65-
if (that.rolling) {
66-
process.nextTick(function() { that.initRolling(); });
67-
}
68-
});
51+
if (this.shouldRoll()) {
52+
this.currentSize = 0;
53+
this.roll(this.filename, writeTheChunk);
54+
} else {
55+
writeTheChunk();
56+
}
6957
};
7058

71-
BaseRollingFileStream.prototype.write = function(data, encoding) {
72-
var canWrite = false;
73-
if (this.rolling) {
74-
this.writesWhileRolling.push({ data: data, encoding: encoding });
75-
} else {
76-
if (this.rollBeforeWrite && this.shouldRoll()) {
77-
this.writesWhileRolling.push({ data: data, encoding: encoding });
78-
this.initRolling();
79-
} else {
80-
canWrite = BaseRollingFileStream.super_.prototype.write.call(this, data, encoding);
81-
this.currentSize += data.length;
82-
debug('current size = ' + this.currentSize);
59+
BaseRollingFileStream.prototype.openTheStream = function(cb) {
60+
debug("opening the underlying stream");
61+
this.theStream = fs.createWriteStream(this.filename, this.options);
62+
if (cb) {
63+
this.theStream.on("open", cb);
64+
}
65+
};
8366

84-
if (!this.rollBeforeWrite && this.shouldRoll()) {
85-
this.initRolling();
86-
}
87-
}
88-
}
89-
return canWrite;
67+
BaseRollingFileStream.prototype.closeTheStream = function(cb) {
68+
debug("closing the underlying stream");
69+
this.theStream.end(null, null, cb);
9070
};
9171

9272
BaseRollingFileStream.prototype.shouldRoll = function() {

lib/streams/BufferedWriteStream.js

Lines changed: 0 additions & 78 deletions
This file was deleted.

lib/streams/DateRollingFileStream.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ function DateRollingFileStream(filename, pattern, options, now) {
2323
debug("this.now is " + this.now + ", now is " + now);
2424

2525
DateRollingFileStream.super_.call(this, filename, options);
26-
this.rollBeforeWrite = true;
2726
}
28-
2927
util.inherits(DateRollingFileStream, BaseRollingFileStream);
3028

3129
DateRollingFileStream.prototype.shouldRoll = function() {
@@ -45,14 +43,12 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
4543
newFilename = filename + this.previousTime;
4644

4745
debug("Starting roll");
48-
debug("Queueing up data until we've finished rolling");
49-
debug("Flushing underlying stream");
50-
this.flush();
5146

5247
async.series([
53-
deleteAnyExistingFile,
54-
renameTheCurrentFile,
55-
openANewFile
48+
this.closeTheStream.bind(this),
49+
deleteAnyExistingFile,
50+
renameTheCurrentFile,
51+
this.openTheStream.bind(this)
5652
], callback);
5753

5854
function deleteAnyExistingFile(cb) {
@@ -69,21 +65,4 @@ DateRollingFileStream.prototype.roll = function(filename, callback) {
6965
fs.rename(filename, newFilename, cb);
7066
}
7167

72-
function openANewFile(cb) {
73-
debug("Opening a new file");
74-
fs.open(
75-
filename,
76-
that.options.flags,
77-
that.options.mode,
78-
function (err, fd) {
79-
debug("opened new file");
80-
var oldLogFileFD = that.fd;
81-
that.fd = fd;
82-
that.writable = true;
83-
fs.close(oldLogFileFD, cb);
84-
}
85-
);
86-
}
87-
88-
8968
};

0 commit comments

Comments
 (0)
0