How do I set log4j level on the command line?

后端 未结 7 904
暖寄归人
暖寄归人 2020-12-24 10:27

I want to add some log.debug statements to a class I\'m working on, and I\'d like to see that in output when running the test. I\'d like to override the log4j properties on

相关标签:
7条回答
  • 2020-12-24 11:02

    With Log4j2, this can be achieved using the following utility method added to your code.

    private static void setLogLevel() {
      if (Boolean.getBoolean("log4j.debug")) {
        Configurator.setLevel(System.getProperty("log4j.logger"), Level.DEBUG);
      }
    }
    

    You need these imports

    import org.apache.logging.log4j.Level;
    import org.apache.logging.log4j.core.config.Configurator;
    

    Now invoke the setLogLevel method in your main() or whereever appropriate and pass command line params -Dlog4j.logger=com.mypackage.Thingie and -Dlog4j.debug=true.

    0 讨论(0)
提交回复
热议问题