简体   繁体   中英

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.

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 :)

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

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.

如果您不想打印,请检查process.env.NODE_ENV并覆盖console.log

console.log = function(){}

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