Open stoberov opened 6 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?
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 :)
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?