Logging in production best practice?

5.2k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.