Dror Cohen
Dror Cohen

Reputation: 6841

java.util.logging: setting the log level dynamically

I'm trying the following code example (based on code from here). My aim is to set the level of the logging from run time.

package logchecker;
import java.util.logging.*;

public class Logchecker {

    private static final Logger logger = Logger.getLogger(Logchecker.class.getName());

   public static void main(String[] args) {
      System.out.println("This logger's level is " + logger.getLevel());   // null
      logger.setLevel(Level.ALL);
      System.out.println("This logger's level is " + logger.getLevel());   // null
      logger.info("TEST");
      logger.finest("FINEST TEST");
   }
}

The output is:

This logger's level is null

This logger's level is ALL

Sep 17, 2013 1:46:31 PM logchecker.Logchecker main

INFO: TEST

It obviously doesn't output the log.finest. What am I missing? I'm running with NetBeans 7.3.

Upvotes: 1

Views: 5467

Answers (1)

Dror Cohen
Dror Cohen

Reputation: 6841

I needed to set my Handlers level as well. Added the following code to my main:

Logger root = Logger.getLogger("");
Handler[] handlers = root.getHandlers();
for(Handler h: handlers){
    h.setLevel(Level.INFO);
}

Of course you can set the level to whatever you need.

thanks again to the comments for directing me to the solution

Upvotes: 1

Related Questions