Search code examples
javascriptnode.jswinston

Winston daily rotate file maxSize is not working


The code is below. Using winston daily rotate file a new file is not being created When the maxSize i.e 2 mb is reached a new file is not created. What could be the problem? Is it a bug of is something wrong with my configuration? I tried adding removing extension or changing file name but it doesnt seem to be working

const customLevels = {
  levels: {
    debug: 3,
    info: 2,
    error: 1,
    critical: 0,
  },
  filename: {
    debug: path.join(
      __dirname,
      `../../logs/debug/%DATE%_debug`
    ),
    info: path.join(
      __dirname,
      `../../logs/info/%DATE%_info`
    ),
    error: path.join(
      __dirname,
      `../../logs/error/%DATE%_error`
    ),
    critical: path.join(
      __dirname,
      `../../logs/critical/%DATE%_critical`
    ),
  },
};

let logger = createLogger({
  levels: customLevels.levels,

  format: format.combine(
    format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss',
    }),
    format.simple()
  ),
  transports: [
    new transports.DailyRotateFile({
      filename: customLevels.filename.debug,
      maxsize: '2m',
      extension: '.log',
      level: 'debug',
    }),

    new transports.DailyRotateFile({
      filename: customLevels.filename.info,
      extension: '.log',
      maxsize: '2m',
      level: 'info',
    }),

    new transports.DailyRotateFile({
      filename: customLevels.filename.error,
      handleExceptions: true,
      maxsize: '2m',
      extension: '.log',
      level: 'error',
    }),

    new transports.DailyRotateFile({
      filename: customLevels.filename.critical,
      handleExceptions: true,
      extension: '.log',
      maxsize: '2m',
      level: 'critical',
    }),
  ],
  // exitOnError: false
});


Solution

  • Your code has the key maxsize where it in fact should be maxSize. Usually keys and binding names are camelCased in JavaScript. You can see this apparent on NPM here:

    https://www.npmjs.com/package/winston-daily-rotate-file

    Where an example is given:

    var transport = new (winston.transports.DailyRotateFile)({
        filename: 'application-%DATE%.log',
        datePattern: 'YYYY-MM-DD-HH',
        zippedArchive: true,
        maxSize: '20m',
        maxFiles: '14d'
      });