winstonjs / winston-daily-rotate-file

A transport for winston which logs to a rotating file each day.
MIT License
889 stars 151 forks source link

DailyFileRotate is not deleting the original log file and upon rotation duplicates into smaller files #333

Open ankityadav1 opened 2 years ago

ankityadav1 commented 2 years ago

Hello team,

I'm trying to use winston and winston-daily-rotate-file for log file rotation if the file size is greater than 5 MB.

I tried to delete the original log file via transport.on('rotate') event but It seems I do not have permission to delete the files or trying to empty the file content doesn't work either.

As per my understanding, winston should delete/empty the original log file after rotation occurs. Correct me if I am wrong If notplease walk me to the best practie to achieve this.

My transport file goes like this: const transport = new DailyRotateFile({ dirname: 'folderPath', filename: 'abc', // no.log as I am mentioning it below extension: '.log', datePattern: 'YYYY-MM-DD-HH', zippedArchive: true, maxSize: 10000000, // 10 MB maxFiles: '3', prepend: true, level: 'info', });

const logFormat = winston.format.combine( winston.format.colorize(), winston.format.align(), winston.format.printf( info =>${moment().tz(moment.tz.guess()).format('MMM DD YYYY, hh:mm:ss A')}-${info.message}, ),) const logger = winston.createLogger({ level: 'info', format: logFormat, transports: [ transport, new winston.transports.File({ filename: 'abc.log' }), ], });

transport.on('rotate', function(oldFilename, newFilename) { fs.unlinkSync(folderPath + '/' + abc.log) fs.writeFile(folderPath + '/' + abc.log,'','UTF-8',function (err) { if (err) console.log('Error in writing to file-->'. err) })

Please let me know if any change required in this. Any help is much appreciated.

DenkevichOleg commented 2 years ago

just dont use winston.transports.File ?