file-stream-rotator
Advanced tools
Comparing version 0.5.0 to 0.5.3
@@ -54,2 +54,7 @@ 'use strict'; | ||
* - `utc` Use UTC time for date in filename. Defaults to 'FALSE' | ||
* | ||
* - `extension` File extension to be appended to the filename. This is useful when using size restrictions as the rotation | ||
* adds a count (1,2,3,4,...) at the end of the filename when the required size is met. | ||
* | ||
* - `watch_log` Watch the current file being written to and recreate it in case of accidental deletion. Defaults to 'FALSE' | ||
* | ||
@@ -284,10 +289,13 @@ * To use with Express / Connect, use as below. | ||
let current = logPath + "/current.log" | ||
fs.lstat(current, function(err, stats){ | ||
if(err && err.code == "ENOENT") { | ||
fs.symlinkSync(logfile, current) | ||
}else if(stats.isSymbolicLink()){ | ||
try { | ||
let stats = fs.lstatSync(current) | ||
if(stats.isSymbolicLink()){ | ||
fs.unlinkSync(current) | ||
fs.symlinkSync(logfile, current) | ||
} | ||
}) | ||
} catch (err) { | ||
if(err && err.code == "ENOENT") { | ||
fs.symlinkSync(logfile, current) | ||
} | ||
} | ||
} | ||
@@ -298,19 +306,27 @@ | ||
* @param {String} logfile | ||
* @param {Boolean} verbose | ||
* @param {function} cb | ||
*/ | ||
function createLogWatcher(logfile, cb){ | ||
function createLogWatcher(logfile, verbose, cb){ | ||
if(!logfile) return null | ||
// console.log("Creating log watcher") | ||
return fs.watch(logfile, function(event,filename){ | ||
// console.log(Date(), event, filename) | ||
if(event == "rename"){ | ||
try { | ||
let stats = fs.lstatSync(logfile) | ||
// console.log("STATS:", stats) | ||
}catch(err){ | ||
// console.log("ERROR:", err) | ||
cb(err,logfile) | ||
} | ||
try { | ||
let stats = fs.lstatSync(logfile) | ||
return fs.watch(logfile, function(event,filename){ | ||
// console.log(Date(), event, filename) | ||
if(event == "rename"){ | ||
try { | ||
let stats = fs.lstatSync(logfile) | ||
// console.log("STATS:", stats) | ||
}catch(err){ | ||
// console.log("ERROR:", err) | ||
cb(err,logfile) | ||
} | ||
} | ||
}) | ||
}catch(err){ | ||
if(verbose){ | ||
console.log(new Date(),"[FileStreamRotator] Could not add watcher for " + logfile); | ||
} | ||
}) | ||
} | ||
} | ||
@@ -387,2 +403,4 @@ | ||
* @param options.utc | ||
* @param options.extension File extension to be added at the end of the filename | ||
* @param options.watch_log | ||
* @returns {Object} stream | ||
@@ -430,2 +448,3 @@ */ | ||
options.extension = options.extension || "" | ||
var filename = options.filename; | ||
@@ -449,3 +468,3 @@ var oldFile = null; | ||
if(lastEntry.match(t_log)){ | ||
var lastCount = lastEntry.match(t_log + "\\.(\\d+)$"); | ||
var lastCount = lastEntry.match(t_log + "\\.(\\d+)"); | ||
// Thanks for the PR contribution from @andrefarzat - https://github.com/andrefarzat | ||
@@ -458,6 +477,11 @@ if(lastCount){ | ||
} | ||
if (fileCount == 0 && t_log == logfile) { | ||
t_log += options.extension | ||
} | ||
while(f = fs.existsSync(t_log)){ | ||
lastLogFile = t_log; | ||
fileCount++; | ||
t_log = logfile + "." + fileCount; | ||
t_log = logfile + "." + fileCount + options.extension; | ||
} | ||
@@ -490,6 +514,15 @@ if(lastLogFile){ | ||
stream.on('close', function(){ | ||
if (logWatcher) { | ||
logWatcher.close() | ||
} | ||
}) | ||
stream.on("new",function(newLog){ | ||
// console.log("new log", newLog) | ||
stream.auditLog = self.addLogToAudit(newLog,stream.auditLog, stream) | ||
createCurrentSymLink(newLog) | ||
stream.emit("addWatcher", newLog) | ||
if(options.watch_log){ | ||
stream.emit("addWatcher", newLog) | ||
} | ||
}); | ||
@@ -502,3 +535,7 @@ | ||
} | ||
logWatcher = createLogWatcher(newLog, function(err,newLog){ | ||
if(!options.watch_log){ | ||
return | ||
} | ||
// console.log("ADDING WATCHER", newLog) | ||
logWatcher = createLogWatcher(newLog, options.verbose, function(err,newLog){ | ||
stream.emit('createLog', newLog) | ||
@@ -531,6 +568,7 @@ }) | ||
fileCount++; | ||
newLogfile += "." + fileCount; | ||
newLogfile += "." + fileCount + options.extension; | ||
}else{ | ||
// reset file count | ||
fileCount = 0; | ||
newLogfile += options.extension | ||
} | ||
@@ -566,2 +604,3 @@ curSize = 0; | ||
}) | ||
stream.emit('new',logfile) | ||
return stream; | ||
@@ -568,0 +607,0 @@ } else { |
{ | ||
"name": "file-stream-rotator", | ||
"version": "0.5.0", | ||
"version": "0.5.3", | ||
"description": "Automated stream rotation useful for log files", | ||
@@ -5,0 +5,0 @@ "main": "FileStreamRotator.js", |
@@ -37,2 +37,6 @@ | ||
- *utc* Use UTC time for date in filename. Defaults to 'FALSE' | ||
- *extension* File extension to be appended to the filename. This is useful when using size restrictions as the rotation | ||
adds a count (1,2,3,4,...) at the end of the filename when the required size is met. | ||
- *watch_log* Watch the current file being written to and recreate it in case of accidental deletion. Defaults to 'FALSE' | ||
@@ -39,0 +43,0 @@ ## Example Usage |
@@ -8,3 +8,3 @@ var moment = require('moment'); | ||
var rotatingLogStream = require('../FileStreamRotator').getStream({ | ||
filename: "/tmp/a/logs/1m/testlog-%DATE%.log", | ||
filename: "logs/1m/testlog-%DATE%", | ||
frequency: "1m", | ||
@@ -17,3 +17,4 @@ verbose: true, | ||
end_stream: false, | ||
utc: true | ||
utc: true, | ||
extension: ".log" | ||
}); | ||
@@ -20,0 +21,0 @@ |
@@ -8,14 +8,18 @@ var moment = require('moment'); | ||
var rotatingLogStream = require('../FileStreamRotator').getStream({ | ||
filename:"/tmp/a/logs/f/testlog-%DATE%.log", | ||
filename:"logs/1s/testlog-%DATE%.log", | ||
frequency:"custom", | ||
verbose: true, | ||
date_format: "YYYY-MM-DD.HH.mm", | ||
size:"50k", | ||
size:"50k", | ||
max_logs: "5", | ||
audit_file:"/tmp/audit.json", | ||
end_stream: true | ||
audit_file:"audit-1s.json", | ||
end_stream: false, | ||
utc: true, | ||
extension: ".logs", | ||
watch_log: true | ||
}); | ||
rotatingLogStream.on("error",function(){ | ||
console.log(Date.now(), Date(), "stream error") | ||
rotatingLogStream.on("error",function(err){ | ||
console.log(Date.now(), Date(), "stream error", err) | ||
process.exit() | ||
}) | ||
@@ -44,2 +48,6 @@ | ||
rotatingLogStream.on("addWatcher", function(newLog){ | ||
console.log(Date.now(), Date(), "stream add watcher",newLog); | ||
}) | ||
// console.log(rotatingLogStream.on, rotatingLogStream.end, rotatingLogStream) | ||
@@ -53,3 +61,3 @@ | ||
// if(counter == 2000){ | ||
if(counter == 200){ | ||
if(counter == 400){ | ||
clearInterval(i); | ||
@@ -56,0 +64,0 @@ console.log(Date() + "\tEND STREAM"); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44682
10
919
139