From 0927e018c20a12ed62ee7efbc03f6c90329f84f7 Mon Sep 17 00:00:00 2001 From: rcastells Date: Fri, 19 Oct 2018 12:24:37 +0100 Subject: [PATCH] fix for #45. supports separate audit files for multiple streams, one per stream --- FileStreamRotator.js | 9 +++++---- package.json | 2 +- tests/every-minute-test.js | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/FileStreamRotator.js b/FileStreamRotator.js index 46c8f11..447a7e4 100644 --- a/FileStreamRotator.js +++ b/FileStreamRotator.js @@ -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; @@ -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 @@ -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); }); diff --git a/package.json b/package.json index f49be63..8977ecb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/tests/every-minute-test.js b/tests/every-minute-test.js index 384aaa8..7c60b2a 100644 --- a/tests/every-minute-test.js +++ b/tests/every-minute-test.js @@ -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);