file-stream-rotator
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -52,2 +52,4 @@ 'use strict'; | ||
* See https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options. Default `{ flags: 'a' }`. | ||
* | ||
* - `utc` Use UTC time for date in filename. Defaults to 'FALSE' | ||
* | ||
@@ -121,3 +123,3 @@ * To use with Express / Connect, use as below. | ||
FileStreamRotator.getFrequency = function (frequency) { | ||
var _f = frequency.toLowerCase().match(/^(\d+)([m|h])$/) | ||
var _f = frequency.toLowerCase().match(/^(\d+)([mh])$/) | ||
if(_f){ | ||
@@ -142,3 +144,3 @@ return _checkNumAndType(_f[2], parseInt(_f[1])); | ||
if(size && typeof size == "string"){ | ||
var _s = size.toLowerCase().match(/^((?:0\.)?\d+)([k|m|g])$/); | ||
var _s = size.toLowerCase().match(/^((?:0\.)?\d+)([kmg])$/); | ||
if(_s){ | ||
@@ -162,15 +164,17 @@ switch(_s[2]){ | ||
* @param date_format | ||
* @param {boolean} utc | ||
* @returns {string} | ||
*/ | ||
FileStreamRotator.getDate = function (format, date_format) { | ||
FileStreamRotator.getDate = function (format, date_format, utc) { | ||
date_format = date_format || DATE_FORMAT; | ||
let currentMoment = utc ? moment.utc() : moment().local() | ||
if (format && staticFrequency.indexOf(format.type) !== -1) { | ||
switch (format.type) { | ||
case 'm': | ||
var minute = Math.floor(moment().minutes() / format.digit) * format.digit; | ||
return moment().minutes(minute).format(date_format); | ||
var minute = Math.floor(currentMoment.minutes() / format.digit) * format.digit; | ||
return currentMoment.minutes(minute).format(date_format); | ||
break; | ||
case 'h': | ||
var hour = Math.floor(moment().hour() / format.digit) * format.digit; | ||
return moment().hour(hour).format(date_format); | ||
var hour = Math.floor(currentMoment.hour() / format.digit) * format.digit; | ||
return currentMoment.hour(hour).format(date_format); | ||
break; | ||
@@ -180,6 +184,6 @@ case 'daily': | ||
case 'test': | ||
return moment().format(date_format); | ||
return currentMoment.format(date_format); | ||
} | ||
} | ||
return moment().format(date_format); | ||
return currentMoment.format(date_format); | ||
} | ||
@@ -278,2 +282,41 @@ | ||
/** | ||
* Create symbolic link to current log file | ||
* @param {String} logfile | ||
*/ | ||
function createCurrentSymLink(logfile) { | ||
let logPath = path.dirname(logfile) | ||
let current = logPath + "/current.log" | ||
fs.lstat(current, function(err, stats){ | ||
if(err && err.code == "ENOENT") { | ||
fs.symlinkSync(logfile, current) | ||
}else if(stats.isSymbolicLink()){ | ||
fs.unlinkSync(current) | ||
fs.symlinkSync(logfile, current) | ||
} | ||
}) | ||
} | ||
/** | ||
* | ||
* @param {String} logfile | ||
* @param {function} cb | ||
*/ | ||
function createLogWatcher(logfile, 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) | ||
} | ||
} | ||
}) | ||
} | ||
/** | ||
* Write audit json object to disk | ||
@@ -287,4 +330,5 @@ * @param {String} logfile | ||
* @param {Array} audit.files | ||
* @param {EventEmitter} stream | ||
*/ | ||
FileStreamRotator.addLogToAudit = function(logfile, audit){ | ||
FileStreamRotator.addLogToAudit = function(logfile, audit, stream){ | ||
if(audit && audit.files){ | ||
@@ -313,2 +357,3 @@ // Based on contribution by @nickbug - https://github.com/nickbug | ||
removeFile(file); | ||
stream.emit("logRemoved", file) | ||
return false; | ||
@@ -322,2 +367,3 @@ }); | ||
removeFile(file); | ||
stream.emit("logRemoved", file) | ||
return false; | ||
@@ -346,2 +392,3 @@ }) | ||
* @param options.file_options | ||
* @param options.utc | ||
* @returns {Object} stream | ||
@@ -377,3 +424,3 @@ */ | ||
} | ||
if(moment().format(dateFormat) != moment().add(2,"hours").format(dateFormat) || moment().format(dateFormat) == moment().add(1,"day").format(dateFormat)){ | ||
if(moment().format(dateFormat) != moment().endOf("day").format(dateFormat) || moment().format(dateFormat) == moment().add(1,"day").format(dateFormat)){ | ||
if(options.verbose){ | ||
@@ -387,3 +434,3 @@ console.log(new Date(),"[FileStreamRotator] Changing type to custom as date format changes more often than once a day or not every day"); | ||
if (frequencyMetaData) { | ||
curDate = (options.frequency ? self.getDate(frequencyMetaData,dateFormat) : ""); | ||
curDate = (options.frequency ? self.getDate(frequencyMetaData,dateFormat, options.utc) : ""); | ||
} | ||
@@ -395,3 +442,3 @@ | ||
if(filename.match(/%DATE%/)){ | ||
logfile = filename.replace(/%DATE%/g,(curDate?curDate:self.getDate(null,dateFormat))); | ||
logfile = filename.replace(/%DATE%/g,(curDate?curDate:self.getDate(null,dateFormat, options.utc))); | ||
} | ||
@@ -450,8 +497,32 @@ var verbose = (options.verbose !== undefined ? options.verbose : true); | ||
stream.on("new",function(newLog){ | ||
stream.auditLog = self.addLogToAudit(newLog,stream.auditLog); | ||
stream.auditLog = self.addLogToAudit(newLog,stream.auditLog, stream) | ||
createCurrentSymLink(newLog) | ||
stream.emit("addWatcher", newLog) | ||
}); | ||
var logWatcher; | ||
stream.on("addWatcher", function(newLog){ | ||
if (logWatcher) { | ||
logWatcher.close() | ||
} | ||
logWatcher = createLogWatcher(newLog, function(err,newLog){ | ||
stream.emit('createLog', newLog) | ||
}) | ||
}) | ||
stream.on("createLog",function(file){ | ||
try { | ||
let stats = fs.lstatSync(file) | ||
}catch(err){ | ||
if(rotateStream && rotateStream.end == "function"){ | ||
rotateStream.end(); | ||
} | ||
rotateStream = fs.createWriteStream(file, file_options); | ||
BubbleEvents(rotateStream,stream); | ||
} | ||
}); | ||
stream.write = (function (str, encoding) { | ||
var newDate = this.getDate(frequencyMetaData,dateFormat); | ||
var newDate = this.getDate(frequencyMetaData, dateFormat, options.utc); | ||
if (newDate != curDate || (fileSize && curSize > fileSize)) { | ||
@@ -458,0 +529,0 @@ var newLogfile = filename + (curDate ? "." + newDate : ""); |
{ | ||
"name": "file-stream-rotator", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Automated stream rotation useful for log files", | ||
@@ -5,0 +5,0 @@ "main": "FileStreamRotator.js", |
@@ -36,4 +36,4 @@ | ||
See https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options. Default `{ flags: 'a' }`. | ||
- *utc* Use UTC time for date in filename. Defaults to 'FALSE' | ||
## Example Usage | ||
@@ -87,2 +87,3 @@ ```javascript | ||
* *new*: that will pass one parameter to the callback: *newFilename* | ||
* *logRemoved*: that will pass one parameter to the callback: {*date*: unix_timestamp, *name*: filename_deleted, *hash*: log_file_unique_idenfifier} | ||
@@ -89,0 +90,0 @@ You can also limit the size of each file by adding the size option using "k", "m" and "g" to specify the size of the file in kiloybytes, megabytes or gigabytes. When it rotates a file based on size, it will add a number to the end and increment it for every time the file rotates in the given period as shown below. |
@@ -15,3 +15,4 @@ var moment = require('moment'); | ||
audit_file: "/tmp/audit.json", | ||
end_stream: false | ||
end_stream: false, | ||
utc: true | ||
}); | ||
@@ -44,2 +45,6 @@ | ||
rotatingLogStream.on("logRemoved", function (newFile) { | ||
console.log(Date.now(), Date(), "stream logRemoved", newFile); | ||
}) | ||
// console.log(rotatingLogStream.on, rotatingLogStream.end, rotatingLogStream) | ||
@@ -69,3 +74,7 @@ | ||
} | ||
}, 10); | ||
}, 100); | ||
// var i = setTimeout(function () { | ||
// rotatingLogStream.emit("createLog") | ||
// }, 10000); | ||
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
42146
860
135