Skip to content

Commit

Permalink
Added event emitter to bubble up the "close","finish" and "error" eve…
Browse files Browse the repository at this point in the history
…nts from the current open writeStream.
  • Loading branch information
rcastells committed Mar 7, 2017
1 parent 628cb26 commit 9d5730c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
26 changes: 24 additions & 2 deletions FileStreamRotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
var fs = require('fs');
var moment = require('moment');

var EventEmitter = require('events');

/**
* FileStreamRotator:
Expand Down Expand Up @@ -137,6 +137,7 @@ FileStreamRotator.getStream = function (options) {
}

var filename = options.filename;
var oldFile = null;
var logfile = filename + (curDate ? "." + curDate : "");
if(filename.match(/%DATE%/)){
logfile = filename.replace('%DATE%',(curDate?curDate:self.getDate(null,dateFormat)));
Expand All @@ -150,7 +151,12 @@ FileStreamRotator.getStream = function (options) {
if (verbose) {
console.log("Rotating file", options.frequency);
}
var stream = {end: rotateStream.end};
var stream = new EventEmitter();
stream.end = function(){
rotateStream.end.apply(rotateStream,arguments);
};
BubbleEvents(rotateStream,stream);

stream.write = (function (str, encoding) {
var newDate = this.getDate(frequencyMetaData,dateFormat);
if (newDate != curDate) {
Expand All @@ -163,9 +169,12 @@ FileStreamRotator.getStream = function (options) {
console.log("Changing logs from %s to %s", logfile, newLogfile);
}
curDate = newDate;
oldFile = logfile;
logfile = newLogfile;
rotateStream.destroy();
rotateStream = fs.createWriteStream(newLogfile, {flags: 'a'});
stream.emit('rotated',oldFile, newLogfile);
BubbleEvents(rotateStream,stream);
}
rotateStream.write(str, encoding);
}).bind(this);
Expand All @@ -176,4 +185,17 @@ FileStreamRotator.getStream = function (options) {
}
return rotateStream;
}
}


var BubbleEvents = function BubbleEvents(emitter,proxy){
emitter.on('close',function(){
proxy.emit('close');
})
emitter.on('finish',function(){
proxy.emit('finish');
})
emitter.on('error',function(err){
proxy.emit('error',err);
})
}
32 changes: 32 additions & 0 deletions tests/every-second-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var moment = require('moment');
var rotatingLogStream = require('file-stream-rotator').getStream({filename:"/tmp/testlog-%DATE%.log", frequency:"daily", verbose: true, date_format: "YYYY-MM-DD.HH.mm.ss"});

rotatingLogStream.on("error",function(){
console.log(Date.now(), Date(), "stream error")
})


rotatingLogStream.on("close",function(){
console.log(Date.now(), Date(), "stream closed")
})

rotatingLogStream.on("finish",function(){
console.log(Date.now(), Date(), "stream finished")
})

rotatingLogStream.on("rotated",function(oldFile,newFile){
console.log(Date.now(), Date(), "stream rotated",oldFile,newFile);
})

// console.log(rotatingLogStream.on, rotatingLogStream.end, rotatingLogStream)

var counter = 0;
var i = setInterval(function(){
counter++;
rotatingLogStream.write("test\n")
if(counter == 20){
clearInterval(i);
rotatingLogStream.end("end\n");
}
}, 100);

0 comments on commit 9d5730c

Please sign in to comment.