bithavoc / express-winston

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

Does express-winston make morgan redundant? #167

Open stoberov opened 6 years ago

stoberov commented 6 years ago

Hey,

Thanks for such an awesome middleware! There's one thing that confuses me though - could you please clarify for me?

"Traditionally", we would use Morgan for logging HTTP requests and then Winston for all other logging needs. Am I right to assume that with express-winston we don't really need morgan any more, because express-winston does the same job and is capable of much more, too?

lonix1 commented 5 years ago

@stoberov Nope

fated-x commented 2 years ago

I discovered this package yesterday and I am currently using morgan. I have just removed the morgan dependency because @stoberov's question back in 2018 is 100% legit and was the first thing I was wondering after reading through the docs for express-winston as well.

I read through the entire discussion that @lonix1 linked but the link is regarding a call for maintainers. I don't see how it's relevant at all unless he was suggesting we drop this package in favor of morgan due to waning interest.

Anyway, this question doesn't seem to be answered and nobody has touched it for 4 years so I am hoping to revive it. I am looking for direction as to whether removing morgan was a mistake. Perhaps one of the active maintainers can straighten this out?

stoberov commented 2 years ago

Hi @fated-x,

For what it's worth, my thoughts 4 years later are:

Frankly, my personal recommendation would be to skip both, just install winston and add a middleware yourself, pretty much what express-winston does. It may sound daunting, but it can end up being as simple as

// logger.js
const { createLogger, format, transports } = require('winston');
const { combine, timestamp, colorize, printf } = format;
const env = process.env.NODE_ENV || 'development';

const consoleTransport = new transports.Console({
  format: combine(
    colorize(),
    timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
  )
});

const logger = createLogger({
  level: env === 'production' ? 'info' : 'debug',
  transports: [consoleTransport], // more transports as desired
  handleExceptions: true
});

module.exports = logger;

// middlewares.js

const requestLogger = (req, res, next) => {
  const { url, method, query, body } = req;

  const request = JSON.stringify({
    url,
    method,
    query,
    body // some parameters may be worth masking/skipping, e.g. passwords
  });

  logger.info(request);

  next();
};

module.exports = {
    requestLogger,
   // more middlewares if needed
}

// index.js
/** Express code here */

server.use(requestLogger);

The main argument I can make for using just winston and adding something like the above is because you're likely to end up needing logging for all kind of other purposes - and you're likely to use winston anyway to create your logging logic. If you'll be doing that, you may as well skip both morgan or express-winston.

Just my $0.02 :)