winston-daily-rotate-file: Could not remove old log file
const winston = require('winston');
require('winston-daily-rotate-file');
const transport = new (winston.transports.DailyRotateFile)({
"name": "basic-log",
"datePattern": "YYYY-MM-DD",
"zippedArchive": false,
"level": "info",
"colorize": false,
"filename": "./logs/log-%DATE%.txt",
"maxSize": "1m",
"maxFiles": "10",
});
transport.on('rotate', function(oldFilename, newFilename) {
// do something fun
console.log(new Date(), oldFilename, newFilename)
});
const logger = new (winston.Logger)({
transports: [
transport
]
});
const str = 'Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World!';
setInterval(function () {
for (let i = 0; i < 100; i++)
logger.info(str);
}, 10);
2018-03-20T12:44:14.690Z ‘logs\log-2018-03-20.txt.4’ ‘logs\log-2018-03-20.txt.5’ 2018-03-20T12:44:15.368Z ‘logs\log-2018-03-20.txt.5’ ‘logs\log-2018-03-20.txt.6’ 2018-03-20T12:44:16.067Z ‘logs\log-2018-03-20.txt.6’ ‘logs\log-2018-03-20.txt.7’ 2018-03-20T12:44:16.751Z ‘logs\log-2018-03-20.txt.7’ ‘logs\log-2018-03-20.txt.8’ 2018-03-20T12:44:17.468Z ‘logs\log-2018-03-20.txt.8’ ‘logs\log-2018-03-20.txt.9’ 2018-03-20T12:44:18.190Z ‘logs\log-2018-03-20.txt.9’ ‘logs\log-2018-03-20.txt.10’ 2018-03-20T12:44:18.869Z ‘logs\log-2018-03-20.txt.10’ ‘logs\log-2018-03-20.txt.11’ 2018-03-20T12:44:19.590Z ‘logs\log-2018-03-20.txt.11’ ‘logs\log-2018-03-20.txt.12’ 2018-03-20T12:44:20.270Z ‘logs\log-2018-03-20.txt.12’ ‘logs\log-2018-03-20.txt.13’ 2018-03-20T12:44:20.968Z ‘logs\log-2018-03-20.txt.13’ ‘logs\log-2018-03-20.txt.14’ 2018-03-20T12:44:20.968Z '[FileStreamRotator] Could not remove old log file: ’ ‘logs\log-2018-03-20.txt.4’ 2018-03-20T12:44:21.689Z ‘logs\log-2018-03-20.txt.14’ ‘logs\log-2018-03-20.txt.15’ 2018-03-20T12:44:22.369Z ‘logs\log-2018-03-20.txt.15’ ‘logs\log-2018-03-20.txt.16’ 2018-03-20T12:44:23.069Z ‘logs\log-2018-03-20.txt.16’ ‘logs\log-2018-03-20.txt.17’ 2018-03-20T12:44:23.790Z ‘logs\log-2018-03-20.txt.17’ ‘logs\log-2018-03-20.txt.18’ 2018-03-20T12:44:24.469Z ‘logs\log-2018-03-20.txt.18’ ‘logs\log-2018-03-20.txt.19’ 2018-03-20T12:44:25.170Z ‘logs\log-2018-03-20.txt.19’ ‘logs\log-2018-03-20.txt.20’ 2018-03-20T12:44:25.870Z ‘logs\log-2018-03-20.txt.20’ ‘logs\log-2018-03-20.txt.21’ 2018-03-20T12:44:26.570Z '[FileStreamRotator] Could not remove old log file: ’ ‘logs\log-2018-03-20.txt.12’ 2018-03-20T12:44:26.570Z ‘logs\log-2018-03-20.txt.21’ ‘logs\log-2018-03-20.txt.22’ 2018-03-20T12:44:27.282Z ‘logs\log-2018-03-20.txt.22’ ‘logs\log-2018-03-20.txt.23’
and with “zippedArchive”: true no file is deleted
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 3
- Comments: 30 (7 by maintainers)
Commits related to this issue
- Deactivate "zippedArchive" option again till https://github.com/winstonjs/winston-daily-rotate-file/issues/125 is fixed because else files are not deleted — committed to ioBroker/ioBroker.js-controller by Apollon77 5 years ago
- fixes https://github.com/winstonjs/winston-daily-rotate-file/issues/125 by also checking for gz filename before deleting — committed to Apollon77/winston-daily-rotate-file by Apollon77 5 years ago
Same here, “maxFiles”: “1d” doesn’t work in my end.
@mattberther Do we have any updates on this?
Hi, Regarding the logrotate: This code works:
In the code above, the files are deleted when the maxFiles is reached.
This code does not work:
used dependencys:
So when zippedArchive is set to true then the files are not deleted after the rotate. I tried also options like maxFiles: 3 and maxFiles: ‘3m’
I can live with that, but it should be nice when this is solved. 😃 Thanks
your example works correctly for me with
"zippedArchive": false(Windows machine).But with
"zippedArchive": true, old files do not get deleted:Stack trace from FileStreamRotator.js
Cherry-picked #260 at https://github.com/winstonjs/winston-daily-rotate-file/commit/e504c1385c1fc7bc7eb82511e182493b4b61fdc4. Published
winston-daily-rotate-file@4.4.1which should resolve this issue.Just tested, the *.gz files are not being removed when it rolls over. I expect them to be removed, so there is a maximum of N-1 zips, and the current log, where N is the max files to keep.
Hi @mattberther. Are you working on the fix for this issue ? I tested it and I think I found at least temporary solution. If you add in FileStreamRotator.js in line 266 this part of code: else { const gzippedFileName = file.name + ‘.gz’ if (fs.existsSync(gzippedFileName)) { fs.unlinkSync(gzippedFileName); } } gZipped files will be removed. It might be a temporary solutions or workaround but it not spoil anything so it might exist.
What do you think about add it to source code?
I’m seeing the same issue here. Error message is written to the console and it actually throws an unhandled error, which takes out the Node server. The *.log.gz files are definitely not being removed as expected.
It works for me if
"maxFiles": "10"set as"maxFiles": 10But when I try with"maxFiles": "1d"i.e. to delete files before 1 day it’s not working.Thanks @chrisgel15. Will monitor that request as well. This particular issue centers on the .gz file not being added to audit.json though. As a result, the .gz files are not being properly pruned.
@CnApTaK @Acionyx Regarding the audit.json file not updating correctly (ie: multiple entries with the same file name) there’s an issue with a dependency called File-Stream-Rotator (https://github.com/rogerc/file-stream-rotator) which is causing that behavior. There’s a Pull Request open to fix it but it has not been integrated yet. (https://github.com/rogerc/file-stream-rotator/pull/37)