java.util.logging.Logger doesn't respect java.util.logging.Level?

前端 未结 6 1604
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 18:14

In plain Java SE 6 environment:

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

Nothing shows up

6条回答
  •  半阙折子戏
    2020-12-02 19:01

    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.

提交回复
热议问题