Winston Logs Rotation Issue

26 Views Asked by At

I am using winston for logging with loggerservice code below.My logs folder structure is logs_track(process.env.LOGS_TRACK)/current date(YYYY-MM-DD)/debug,others,requestResponse(three subfolders).Now debug has a file with per day rotational logs and others,requestRespone folder has info and error logs rotated every hour. EXAMPLES :-

  1. logs_track/2023-11-01/debug/debug-2023-11-01
  2. logs_track/2023-11-01/others/info-2023-11-01-13(hour)
  3. logs_track/2023-11-01/requestResponse/info-2023-11-01-13
export class LoggerTrackService {
  logger: any;
  constructor(private readonly configData: ConfigurationService) {
    const currentDate = new Date().toISOString().split('T')[0];
    this.logger = winston.createLogger({
      transports: [
        new DailyRotateFile({
          filename: path.join(process.env.LOGS_TRACK, currentDate, 'others', 'info-%DATE%.log'),
          datePattern: 'YYYY-MM-DD-HH',
          zippedArchive: true,
          maxSize: '80m',
          level: 'info',
          handleExceptions: false,
          format: winston.format.printf((info) => {
            // console.log(">>>>>>>>INFO DATES", DateService.currentDateObj());
            return `[${DateService.currentDateObj()}] [${info.label}] [${info.level}]: ${
              typeof info.message === 'object' ? JSON.stringify(info.message) : info.message
            }`;
          }),
        }),
        
        new DailyRotateFile({
          // new Date().toISOString().slice(0, 10)
          filename: path.join(process.env.LOGS_TRACK,currentDate ,'others','info-%DATE%.log'),
          datePattern: 'YYYY-MM-DD-HH',
          zippedArchive: true,
          maxSize: '80m',
          level: 'error',
          handleExceptions: false,
          format: winston.format.printf(
            info =>
              `[${DateService.currentDateObj()}] [${info.label}] [${info.level}]: ${
                typeof info.message === 'object' ? JSON.stringify(info.message) : info.message
              }`,
          ),
          
        }),
        new DailyRotateFile({
          filename: path.join(process.env.LOGS_TRACK, currentDate,'debug','debug-%DATE%.log'),
          datePattern: 'YYYY-MM-DD',
          zippedArchive: true,
          maxSize: '100m',
          level: 'debug',
          handleExceptions: false,
          format: winston.format.printf(
            info =>
              `[${DateService.currentDateObj()}] [${info.label}] [${info.level}]: ${
                typeof info.message === 'object' ? JSON.stringify(info.message) : info.message
              }`,
          ),
        }),
      ],
      exitOnError: true,
    });
    if (process.env.DEPLOYMENT_ENV !== 'prod') {
      this.logger.add(
        new winston.transports.Console({
          format: winston.format.simple(),
        }),
      );
    }
  }
}

My issue is future dates logs are generated in current date for debug folder(Refer Image-1.2 and check modified date and filename). Eg logs of 2nd,3rd Nov are generated on 1st Nov folder itself Another issue is for others folder future date logs with same time as of current is getting appended on the same log file(Refer Image 1.1).

IMAGE 1.1

IMAGE 1.2

Any lead for the issue or any suggestion regarding folder structure will be helpful

0

There are 0 best solutions below