Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4js: logs depend on NODE_ENV

How can I make logs (INFO, ERROR) dependent on set NODE_ENV?

I mean, for example, if NODE_ENV=development, I write only ERROR logs. With NODE_ENV=production, there has to be only INFO.

How should I modify appenders to perform this?

Thanks for your help.

like image 261
a-a Avatar asked Mar 10 '26 01:03

a-a


1 Answers

With Log4js it looks like you just need to set the level on the logger depending on the environment variable, e.g.

var logger = log4js.getLogger('myLogger');
if (process.env.NODE_ENV === 'production') {
  logger.setLevel('ERROR');
} else {
  logger.setLevel('INFO');
}

Note that I switched your log levels around as how most logging works you want increasing levels of severity with ERROR being more serious than INFO. In production you want only the most serious errors to be logged. In development you want to see serious errors as well as logs that are just for information.

like image 192
piemonkey Avatar answered Mar 12 '26 14:03

piemonkey