1 Reply Latest reply: Aug 21, 2008 4:34 PM by 807589 RSS

    How to change log level in log4j at runtime

    807589
      Hi,

      I have a small requirement where the default log level is info, but if the user wants to invoke the Java program with debug level, the Debug should start off. I am trying to achieve this using this code..
                Logger root = Logger.getRootLogger();
                root.setLevel(Level.DEBUG);
                log.debug("This is debug");
      When I do this, the debug statement at the end does not print.
      Then I thought may be the root logger level is not propagated after the initialization at start up, so I tried this
                log.setLevel(Level.DEBUG);
                log.info("This is info");
                log.debug("This is debug");
                log.info("Log level is " + log.getLevel());
                log.info(log.isDebugEnabled());
      The info statement prints "this is info" then the next info statement prints "log level is DEBUG" the next statement prints true but the intermediate debug statement does not print.

      I have done this before in a Web app and it works just fine. I do not understand what is going on. Can someone please help me understand this.
        • 1. Re: How to change log level in log4j at runtime
          807589
          I am not really familiar with Log4J, but in java.util.logging you define both a loglevel on the logger and the Handler that is actually responsible for outputting. A quick look on the Log4J sites shows that those are called 'appenders' in Log4J. So you might want to look if your appender is actually configured to output DEBUG-level statements.