Java: low level logging messages not showing








FINE level logging message is not shown even if the handler.setLevel(Level.FINE).

Reason

java.util.logging has a root logger that defaults to Level.INFO, and a ConsoleHandler attached to it that also defaults to Level.INFO. Level.FINE is lower than Level.INFO, so fine messages are not displayed by default.

Solution

    Logger logger = Logger.getLogger("my-logger");
    logger.setLevel(Level.ALL);
    logger.setUseParentHandlers(false);
    /*
     * file handler
     */
    FileHandler fileHandler = new FileHandler(
                "%h/.my-app.%g.log", 5000000, 2);
    fileHandler.setLevel(Level.ALL);
    logger.addHandler(fileHandler);
    /*
     * console handler
     */
    ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setLevel(Level.INFO);
    logger.addHandler(consoleHandler);



No comments:

Post a Comment