bithavoc / express-winston

express.js middleware for winstonjs
https://www.npmjs.com/package/express-winston
MIT License
796 stars 187 forks source link

Multiple transports causes logger to fail #208

Open yellowwaterbeest opened 5 years ago

yellowwaterbeest commented 5 years ago

I am using the winston express logging. I have this configuration that works:

app.use(expressWinston.logger({
  transports: [
    new logger.transports.File({
      json: true,
      filename: config.get('accesslog.directory') + config.get('accesslog.file'),
      level: config.get('log.level'),
      maxsize: 20971520,
      maxFiles: 3
    })
  ]
}));

What I want is to have 2 different loggers. 1 logger for the regular messages and another logger that will log everytime someone logs in. This also must be logged to a different file.

app.use(expressWinston.logger({
  transports: [
    new logger.transports.File({
      json: true,
      filename: config.get('accesslog.directory') + config.get('accesslog.file'),
      level: 'info',
      maxsize: 20971520,
      maxFiles: 3
    })
    ,
    new logger.transports.File({
      json: true,
      filename: 'auth.log',
      level: 'debug',
      maxsize: 20971520,
      maxFiles: 3
    })
  ]
}));

When I add the 2nd transport the logging crashes because no log messages appear. There is no error in the console either. Why can't I add a second transport?

yinzara commented 4 years ago

No idea :) You should be able to use as many transports as you want. I can say that there is a bug in winston that if a file transport errors a single time, it gets removed from the active transports. Winston 3.2.0 and higher have the fix.

The better debug, create the winston instance separately from the expressWinston logger and add a function to handle the error event:

const winston = require("winston");
const expressWinston = require("express-winston");

const logger = winston.createLogger({
  transports: [
    new logger.transports.File({
      json: true,
      filename: config.get('accesslog.directory') + config.get('accesslog.file'),
      level: 'info',
      maxsize: 20971520,
      maxFiles: 3
    })
    ,
    new logger.transports.File({
      json: true,
      filename: 'auth.log',
      level: 'debug',
      maxsize: 20971520,
      maxFiles: 3
    })
  ]
});
logger.addEventListener("error", error => {
   console.error(error);
});
app.use(expressWinston.logger({winstonInstance: logger});

That should at least show you what's erring in the transport.