简体   繁体   中英

Logging in production best practice?

Is it alright to use morgan as logger on production mode, or just throw it away and only use it on development mode?
What's the best practice for logging on production mode?

Yes! It is all right to use Morgan as a logger on production mode.

Arguably, if I can generalize to answer your question, the best practice in production is to log as many details as possible . The idea is that the logs on your server show you as much relevant information as you need. After all, you and the people with access to the server are the only one who sees them, right?

A strategy I use is a 'combined' mode in production, which is a bit more detailed, and a 'dev' mode in development, which is more concise.

You can switch those easily with environmental variable or whatever. Example:

if (app.get('env') === 'production') {
  app.use(logger('combined'));
} else {
  app.use(logger('dev'));
}

Another thing I always configure is writing the logs in an external file . Needless to say why this is a nice to have, in production.

That's it in terms of Morgan. If you are wondering about the best for logging in general, well, that's another question which is already answered .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM