15

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?

Terry Djony
  • 1,975
  • 4
  • 23
  • 41
  • 1
    I think its fine to use morgan on production because it keeps the log file and from there you can get the error if something bad happens. – NewUser Dec 14 '17 at 13:40
  • @NewUser So, it's better to log in file than console on production mode? – Terry Djony Dec 14 '17 at 13:41
  • yes I think so. Thats my personal opinion. – NewUser Dec 14 '17 at 13:43
  • Logs are often written to stdout in modern fancy web apps that are hosted in the cloud. See [here](https://12factor.net/logs). The level to log at is usually different between development and production too. – Mika Sundland Dec 14 '17 at 13:52
  • 2
    I use `pm2` with `pm2-logrotate` and then I just `console.log` and `console.error` everywhere and let PM2 deal with it. This is a matter of opinion, preference and needs depending on the project. – damd Dec 14 '17 at 13:53

1 Answers1

9

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.

Kaloyan Kosev
  • 12,483
  • 8
  • 59
  • 90