JDK: how to enable PlatformLogger programmatically

前端 未结 2 1051
没有蜡笔的小新
没有蜡笔的小新 2021-02-14 09:34

I need to enable logging for some JDK7 internal class programmatically.

This is what I do in the initialization of my application:

httpLogger = Logger.ge         


        
2条回答
  •  余生分开走
    2021-02-14 10:23

    My solution that appears at the beginning of a small custom widget demo program:

    // Log all focus messages.
    final Logger rootLogger = Logger.getLogger("");
    rootLogger.setLevel(Level.ALL);
    final ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setLevel(Level.ALL);
    // Because there are a lot of focus messages, make them
    // a little easier to read by logging only the message.
    consoleHandler.setFormatter(new Formatter() {
        @Override
        public String format(LogRecord record) {
            return "FOCUS: " + record.getMessage() + '\n';
        }
    });
    final Logger logger = Logger.getLogger("java.awt.focus.Component");
    logger.setLevel(Level.ALL);
    logger.setUseParentHandlers(false);
    logger.addHandler(consoleHandler);
    

    It's unclear to me why I needed to get then set the rootLogger level to ALL, but that's what I needed.

提交回复
热议问题