Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does express-winston make morgan redundant? #167

Open
stoberov opened this issue Feb 15, 2018 · 3 comments
Open

Does express-winston make morgan redundant? #167

stoberov opened this issue Feb 15, 2018 · 3 comments
Labels

Comments

@stoberov
Copy link

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
Copy link

lonix1 commented Apr 8, 2019

@stoberov Nope

@fated-x
Copy link

fated-x commented Mar 15, 2022

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
Copy link
Author

stoberov commented Mar 15, 2022

Hi @fated-x,

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

  • morgan is the most recommended approach (in tutorials) to log incoming requests, just because it's absolutely the easiest possible way to do it. It does the job with 1 line of code. Nothing wrong with that.
  • express-winston does absolutely everything morgan can do. It is only a little "harder" than morgan, because express-winston needs a few lines of configuration. It is not a "mistake" to replace morgan.

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants