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

Winston-daily-rotate-file is not updating the oldfileName argument of rotate event listener after renaming the file #349

Open shbaskar opened 2 years ago

shbaskar commented 2 years ago

We are using winston-daily-rotate node.js module for logging. We are maintaining the logs date wise folder. So whenever the date changes, we are using rotate event listener and moving the file to new date folder by using the rename functionality. After renaming the file in rotate event call back function, the next rotate event is getting older filename but not the renamed filename.

Below is my code for winston rotate module producing logs on 24hr basis.

what changes i need to do in below code?

  var installedDate=getTodayDate();        // will give the date when service was started
  let fileTransport = new (winston.transports.DailyRotateFile)({
            filename: getININLogPath(),        //will give the path to save log files.
            datePattern: 'YYYY-MM-DD',
            zippedArchive: true,
            maxSize: '20k',// size=20kb
            maxFiles: '14d'
  });
  fileTransport.setMaxListeners(30);

  fileTransport.on('rotate', (oldFilename, newFilename) => {
      let currentDate=getTodayDate(); //getTodayDate will give the current date.
      let newFile = getININLogPath(); // will give the path to save log files.
      if (currentDate!=installedDate && process.env.ININ_TRACE_ROOT) {
          installedDate = currentDate; //compare the old date with new one and then rename old date folder to new one
          fs.rename(newFilename, newFile, (err) => {
              // eslint-disable-next-line no-console
              if (err) { console.log('Failed to move the new file into todays folder', err); }
              else { console.log('Successfully renamed file name to ', newFile); }
          });
      }
  });

But this after renaming function (fs.rename(newFilename, newFile, (err)) its coming in previous date folder ex-if installed date is 2022-05-23 and next 2022-05-24 , logs of 24th also coming in 23th date.

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

wbt commented 2 years ago

Historically, you may be more likely to get a quicker answer to this usage question on StackOverflow than here.