Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving fix deleting files #113

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,19 +807,22 @@ DailyRotateFile.prototype._getTime = function (timeType) {
// based on "maxDays" option
DailyRotateFile.prototype._cleanOldFiles = function () {
var self = this;
var millisecondsInDay = 86400000;
var millisecondsInOneDay = 86400000;
var now = Date.now();

function removeOldFile(file) {
fs.unlink(self.dirname + path.sep + file, function (errUnlink) {
var completeFileName = path.join(self.dirname, file);

fs.unlink(completeFileName, function (errUnlink) {
if (errUnlink) {
console.error('Error removing file ', file);
}
});
}

function tryToRemoveLogFile(file) {
var completeFileName = self.dirname + path.sep + file;
var completeFileName = path.join(self.dirname, file);

fs.stat(completeFileName, function (errStats, stats) {
if (errStats) {
console.error('Error stats file ', file, errStats);
Expand All @@ -828,7 +831,8 @@ DailyRotateFile.prototype._cleanOldFiles = function () {

var lastChangeTimestamp = ((stats.mtime && stats.mtime.getTime()) || 0);
var lifeTime = now - lastChangeTimestamp;
if (stats.isFile() && lifeTime > (millisecondsInDay * self.maxDays)) {

if (stats.isFile() && lifeTime > (millisecondsInOneDay * self.maxDays)) {
removeOldFile(file);
}
});
Expand All @@ -842,10 +846,10 @@ DailyRotateFile.prototype._cleanOldFiles = function () {
return;
}

var fileNameReg = new RegExp(self._basename, 'g');
files.forEach(function (file) {
if (/.log/.test(file) && fileNameReg.test(file)) {
tryToRemoveLogFile(file);
// for self._basename as "myapp.log", it catchs 'myapp.log', 'myapp.log.1', 'myapp.log.21.09.2017'
if (file.startsWith(self._basename)) {
tryToRemoveLogFile();
}
});
});
Expand Down