winstonjs / winston-daily-rotate-file

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

Duplicate entries in 'info' file for 'warn' and 'error' output. #298

Closed ChrisMiami closed 4 years ago

ChrisMiami commented 4 years ago

Application has 2 DailyRotateFile transports created, one at level: info and one at level: warn. When a log message is written with log.warn('blabla'); there are 2 lines written to the info file with the exact same timestamp.

version: 3.10.0 (presumably still in 4.5.0, since commit messages don't mention fixing it)

Here is my config:

const info_log = new transports.DailyRotateFile({
  maxsize: 10 * 1000 * 1024, // 10 MB
  maxFiles: '61d',   // logger keeps 1 less file than this, so to keep 60 days, use 61 :-\
  tailable: true,
  level: 'info',
  dirname: cfg.logDir,
  filename: `${appName}-info-%DATE%.log`,   // winston replaces the %DATE%
  zippedArchive: false,
  datePattern: 'YYYY-MM-DD',
  auditFile: `${cfg.logDir}${appName}-info.audit`,  // let's see if we can do without this silly file?
});

const error_log = new transports.DailyRotateFile({
  maxsize: 10 * 1000 * 1024, // 10 MB
  maxFiles: '61d',   // logger keeps 1 less file than this, so to keep 60 days, use 61 :-\
  tailable: true,
  level: 'warn',
  dirname: cfg.logDir,
  filename: `${appName}-info-%DATE%.log`,   // winston replaces the %DATE%
  zippedArchive: false,
  datePattern: 'YYYY-MM-DD',
  auditFile: `${cfg.logDir}${appName}-error.audit`,
});

const logger = createLogger({
  level: 'info',
  transports: [
    info_log,
    error_log
  ],
  // Log exceptions to a special file
  exceptionHandlers: [
      new transports.File({
        maxsize: 10 * 1000 * 1024, // 10 MB
        maxFiles: 100,
        tailable: true,
        filename: `${cfg.logDir}${appName}-exceptions.log`,
    })
  ]
});
mattberther commented 4 years ago

@ChrisMiami First, I'd like to point out that your configuration appears to duplicate the log files for both transports. Both seem to use the same dirname and filename properties. Assuming that's not the issue here, let's move on.

I believe this is working as expected. Consistent with https://github.com/winstonjs/winston#using-logging-levels, the logging level is used to specify the maximum level of messages the transport should log. Since info is higher than warn, I'd expect a call to log.warn(...) to log to both the info_log and error_log transports provided in your configuration. However, I'd expect that a call to log.info(...) to NOT log to your error_log transport.

I've validated that this works properly on my side - however, if you continue to have problems, don't hesitate to reopen this issue.