winston-daily-rotate-file
Advanced tools
Comparing version 1.4.6 to 1.5.0
52
index.js
@@ -83,2 +83,3 @@ 'use strict'; | ||
this.zippedArchive = options.zippedArchive || false; | ||
this.maxDays = options.maxDays || 0; | ||
@@ -441,2 +442,3 @@ if (this.json) { | ||
this._filenameHasExpired()) { | ||
this._cleanOldFiles(); | ||
// | ||
@@ -777,1 +779,51 @@ // If we dont have a stream or have exceeded our size, then create | ||
}; | ||
// ### @private function _cleanOldFiles () | ||
// Remove old log files | ||
// based on "maxDays" option | ||
DailyRotateFile.prototype._cleanOldFiles = function () { | ||
var self = this; | ||
var millisecondsInDay = 86400000; | ||
var now = Date.now(); | ||
function removeOldFile(file) { | ||
fs.unlink(self.dirname + path.sep + file, function (errUnlink) { | ||
if (errUnlink) { | ||
console.error('Error removing file ', file); | ||
} | ||
}); | ||
} | ||
function tryToRemoveLogFile(file) { | ||
var completeFileName = self.dirname + path.sep + file; | ||
fs.stat(completeFileName, function (errStats, stats) { | ||
if (errStats) { | ||
console.error('Error stats file ', file, errStats); | ||
return; | ||
} | ||
var lastChangeTimestamp = ((stats.mtime && stats.mtime.getTime()) || 0); | ||
var lifeTime = now - lastChangeTimestamp; | ||
if (stats.isFile() && lifeTime > (millisecondsInDay * self.maxDays)) { | ||
removeOldFile(file); | ||
} | ||
}); | ||
} | ||
// if not maxDays specified, do not remove old log files | ||
if (self.maxDays) { | ||
fs.readdir(self.dirname, function (err, files) { | ||
if (err) { | ||
console.error('Error reading directory ', self.dirname, err); | ||
return; | ||
} | ||
var fileNameReg = new RegExp(self._basename, 'g'); | ||
files.forEach(function (file) { | ||
if (/.log/.test(file) && fileNameReg.test(file)) { | ||
tryToRemoveLogFile(file); | ||
} | ||
}); | ||
}); | ||
} | ||
}; |
{ | ||
"name": "winston-daily-rotate-file", | ||
"version": "1.4.6", | ||
"version": "1.5.0", | ||
"description": "A transport for winston which logs to a rotating file each day.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,4 +12,4 @@ # winston-daily-rotate-file | ||
require('winston-daily-rotate-file'); | ||
var transport = new winston.transports.DailyRotateFile({ | ||
var transport = new (winston.transports.DailyRotateFile)({ | ||
filename: './log', | ||
@@ -20,3 +20,3 @@ datePattern: 'yyyy-MM-dd.', | ||
}); | ||
var logger = new (winston.Logger)({ | ||
@@ -37,2 +37,3 @@ transports: [ | ||
* __zippedArchive:__ A boolean to define whether or not to gzip archived log files (default 'false'). | ||
* __maxDays:__ A number representing the maximum number of days a log file will be saved. Any log file older than this specified number of days will be removed. If not value or a 0, no log files will be removed. | ||
@@ -39,0 +40,0 @@ Valid meta characters in the datePattern are: |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
59470
1444
65
2