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

Most upvoted comments

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:

static logger = createLogger({
        level: config.get('logLevel'),
        format: format.combine(format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss'}), format.json()),
        defaultMeta: { service: 'EHRM-KLANTNR'},
        transports: [
            new DailyRotateFile({
                filename: 'error-%DATE%.log',
                dirname: './logs',
                datePattern: 'YYYY-MM-DD-HH-mm',
                zippedArchive: false,
                level: 'error',
                maxFiles: '3',
                maxSize: '20m',
             }),
            new transports.Console,
            new transports.File({ filename: 'error.log'}),
           // new transports.File({ filename: 'errordebug.log', level: 'debug'})
        ]
    });

In the code above, the files are deleted when the maxFiles is reached.

This code does not work:

        level: config.get('logLevel'),
        format: format.combine(format.timestamp({format: 'YYYY-MM-DDTHH:mm:ss'}), format.json()),
        defaultMeta: { service: 'EHRM-KLANTNR'},
        transports: [
            new DailyRotateFile({
                filename: 'error-%DATE%.log',
                dirname: './logs',
                datePattern: 'YYYY-MM-DD-HH-mm',
                zippedArchive: true,
                level: 'error',
                maxFiles: '3',
                maxSize: '20m',
            }),
            new transports.Console,
            new transports.File({ filename: 'error.log'}),
           // new transports.File({ filename: 'errordebug.log', level: 'debug'})
        ]
    });

used dependencys:

"winston": "^3.2.1",
"winston-daily-rotate-file": "^3.9.0"

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:

2018-03-20T16:39:14.970Z '[FileStreamRotator] Could not remove old log file: ' 'logs\\log-2018-03-20.txt.2' 

Stack trace from FileStreamRotator.js

Error: ENOENT: no such file or directory, unlink '<XXXX>\logs\log-2018-03-20.txt.2'
    at Error (native)
    at Object.fs.unlinkSync (fs.js:1103:18)
    at removeFile (<XXXX>\node_modules\file-stream-rotator\FileStreamRotator.js:258:16)
    at <XXXX>\node_modules\file-stream-rotator\FileStreamRotator.js:298:21

Cherry-picked #260 at https://github.com/winstonjs/winston-daily-rotate-file/commit/e504c1385c1fc7bc7eb82511e182493b4b61fdc4. Published winston-daily-rotate-file@4.4.1 which 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": 10 But 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)