3

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.

a-a
  • 472
  • 2
  • 6
  • 20

3 Answers3

2

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.

piemonkey
  • 741
  • 3
  • 6
2

I have found more appropriate (for me) solution of this problem. Just configure categories in levels in this way:

...
      "levels": {
        "[all]": "INFO",
        "console": (env == "production" ? "ERROR" :"INFO")
      },
...

I needed to think a little before rushing to stackoverflow :)

a-a
  • 472
  • 2
  • 6
  • 20
-1

Check process.env.NODE_ENV and override console.log if you don't want it to print.

console.log = function(){}
Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • 1
    This would be a heavy handed way of doing things and would at best only allow two levels of logging (as error logging could be done with log4js) or if only using console.log it would be on or off without allowing for levels. – piemonkey Oct 20 '15 at 11:15