Skip to content

Commit

Permalink
fix for #45. supports separate audit files for
Browse files Browse the repository at this point in the history
multiple streams, one per stream
  • Loading branch information
rcastells committed Oct 19, 2018
1 parent 5fd769e commit 0927e01
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
9 changes: 5 additions & 4 deletions FileStreamRotator.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ FileStreamRotator.getStream = function (options) {
frequencyMetaData = self.getFrequency(options.frequency);
}

self.auditLog = self.setAuditLog(options.max_logs, options.audit_file, options.filename);
let auditLog = self.setAuditLog(options.max_logs, options.audit_file, options.filename);

var fileSize = null;
var fileCount = 0;
Expand Down Expand Up @@ -387,8 +387,8 @@ FileStreamRotator.getStream = function (options) {
var lastLogFile = null;
var t_log = logfile;
var f = null;
if(self.auditLog && self.auditLog.files && self.auditLog.files instanceof Array && self.auditLog.files.length > 0){
var lastEntry = self.auditLog.files[self.auditLog.files.length - 1].name;
if(auditLog && auditLog.files && auditLog.files instanceof Array && auditLog.files.length > 0){
var lastEntry = auditLog.files[auditLog.files.length - 1].name;
if(lastEntry.match(t_log)){
var lastCount = lastEntry.match(t_log + "\\.(\\d+)$");
// Thanks for the PR contribution from @andrefarzat - https://github.com/andrefarzat
Expand Down Expand Up @@ -422,13 +422,14 @@ FileStreamRotator.getStream = function (options) {
console.log(new Date(),"[FileStreamRotator] Rotating file: ", frequencyMetaData.type);
}
var stream = new EventEmitter();
stream.auditLog = auditLog;
stream.end = function(){
rotateStream.end.apply(rotateStream,arguments);
};
BubbleEvents(rotateStream,stream);

stream.on("new",function(newLog){
self.auditLog = self.addLogToAudit(newLog,self.auditLog);
stream.auditLog = self.addLogToAudit(newLog,stream.auditLog);

});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "file-stream-rotator",
"version": "0.3.1",
"version": "0.4.0",
"description": "Automated stream rotation useful for log files",
"main": "FileStreamRotator.js",
"scripts": {
Expand Down
14 changes: 14 additions & 0 deletions tests/every-minute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,27 @@ rotatingLogStream.on("new", function (newFile) {

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


var rotatingLogStream1 = require('../FileStreamRotator').getStream({
filename: "/tmp/a/logs/1m-1/testlog-%DATE%.log",
frequency: "1m",
verbose: true,
date_format: "YYYY-MM-DD.HH.mm",
size: "50k",
max_logs: "10",
audit_file: "/tmp/audit-1.json",
end_stream: false
});

var counter = 0;
var i = setInterval(function () {
counter++;
rotatingLogStream.write(Date() + "\t" + "testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890\n")
rotatingLogStream1.write(Date() + "\t" + "testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890-testing 1234567890\n")
if (counter == 5000) {
clearInterval(i);
rotatingLogStream.end("end\n");
rotatingLogStream1.end("end\n");
}
}, 10);

0 comments on commit 0927e01

Please sign in to comment.