tags:

views:

1795

answers:

3

In plain JSE 6 environment:

Logger l = Logger.getLogger("nameless");
l.setLevel(Level.ALL);
l.fine("somemessage");

Nothing shows up in Eclipse console. l.info("") and above works just fine, but anything below fine just doesn't seem to work. What's could be wrong? TIA.

+6  A: 

Even though the Logger level is set to ALL, the ConsoleHandler (the default Handler on the logger) still has a default level of INFO. This comes from the default logging.properties in *JAVA_HOME/jre/lib*

Kevin
+3  A: 

In stead of looping through all handlers and set the logging level, i prefer to set only the level of the console handler:

    //get the top Logger:
    Logger topLogger = java.util.logging.Logger.getLogger("");

    // Handler for console (reuse it if it already exists)
    Handler consoleHandler = null;
    //see if there is already a console handler
    for (Handler handler : topLogger.getHandlers()) {
        if (handler instanceof ConsoleHandler) {
            //found the console handler
            consoleHandler = handler;
            break;
        }
    }


    if (consoleHandler == null) {
        //there was no console handler found, create a new one
        consoleHandler = new ConsoleHandler();
        topLogger.addHandler(consoleHandler);
    }
    //set the console handler to fine:
    consoleHandler.setLevel(java.util.logging.Level.FINEST);
michel.iamit
Much better than my answer. +1
Michael Myers
Thanx, but I agree the real answer is in setting the logging level, like in the answer above. This solution is more like a workaround. I don't get why you cannot set the logging level in the console settings in Eclise. I used to work with JBuilder, and there this was an option.
michel.iamit
A: 

An indivual at my workplace found the following to work:

public class Foo {
    private final static Logger logger = Logger.getLogger(Foo.class.getName());
    public static final void main(String[] args) {
        ConsoleHandler ch = new ConsoleHandler();
        ch.setLevel(Level.FINEST);
        Foo.logger.addHandler(ch);
        Foo.logger.setLevel(Level.FINEST);
        Foo.logger.finest("test");
    }
}

If you just set the root or the handler to finest (exclusively) then it didn't work. When I set both to FINEST then it works. His explanation was:

Both the logger and its handlers have Log Levels… The order of filtering is Logger then Handlers. That means it checks to see if the log message passes the loggers filter first, then sends the message on to the individual handlers for filtering.

He further explained it using the following examples

Logger myLogger has a level of FINEST and a single ConsoleHandler myHandler which has a level of INFO

myLogger.fine(“foo”) à message makes it past the logger’s filter, but gets stopper by the handler’s filter… Nothing output.

myLogger.info(“foo”) à passes both filters and “foo” is output.

Now…

Logger myLogger has a level of INFO and a single ConsoleHandler myHandler which has a level of FINEST

myLogger.fine(“foo”) à message gets stopped by the logger’s filter and never makes it to the handler… Nothing output.

myLogger.info(“foo”) à passes both filters and “foo” is output.

Now…

Logger myLogger has a level of FINEST and a single ConsoleHandler myHandler which has a level of FINEST

myLogger.fine(“foo”) à passes both filters and “foo” is output.

myLogger.info(“foo”) à passes both filters and “foo” is output.

jwmajors81