Share this page 

Change the logging level on-the-fly (Log4J)Tag(s): Environment


While you can set the logging level through the configuration properties file
; can be OFF, FATAL, ERROR, WARN, INFO, DEBUG, ALL
log4j.threshold=WARN
It may be useful to change it on-the-fly for debugging purpose. You need to get the Log4J "root" Logger and then change its "level".
...
    setLogLevel("DEBUG");
...
    setLogLevel("INFO");
...

private void setLogLevel(String level) {
   Logger root = Logger.getRootLogger();
   if ("DEBUG".equals(level)) {
      root.setLevel(org.apache.log4j.Level.DEBUG);
   }
   else {
      root.setLevel(org.apache.log4j.Level.INFO);
   }
}