启用Java Swing日志记录(键分派)

5

我需要调试Swing应用程序中的按键事件分发。我认为以下内容已经足够:

val eventLog = PlatformLogger.getLogger("java.awt.event.Component")
eventLog.setLevel(PlatformLogger.Level.ALL)
val focusLog = PlatformLogger.getLogger("java.awt.focus.DefaultKeyboardFocusManager")
focusLog.setLevel(PlatformLogger.Level.ALL)

但是什么也没有发生。(记录器报告它们已启用,但我看不到任何文本输出)。我需要在某个地方配置PrintStream才能看到日志消息吗?

1个回答

6

这里的建议,我不确定是否适合使用sun.util.logging.PlatformLogger。为了记录焦点事件,我必须在根日志记录器上指定更高的级别,例如Level.ALL。像这个示例一样添加ConsoleHandler可能会使日志更易于阅读。

控制台:

java.awt.focus.DefaultKeyboardFocusManager: sun.awt.TimedWindowEvent[WINDOW_GAINED_FOCUS,opposite=null,oldState=0,newState=0] on frame0
java.awt.focus.DefaultKeyboardFocusManager: java.awt.event.WindowEvent[WINDOW_ACTIVATED,opposite=null,oldState=0,newState=0] on frame0
java.awt.focus.DefaultKeyboardFocusManager: tempLost {0}, toFocus {1}
java.awt.focus.Component: 焦点所有者为null或为本身
java.awt.focus.DefaultKeyboardFocusManager: 在{0}处为{1}排队
java.awt.focus.Component: 传递给javax.swing.JButton[,0,0,97x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@56e325b9,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Button 1,defaultCapable=true]
java.awt.focus.DefaultKeyboardFocusManager: java.awt.event.WindowEvent[WINDOW_OPENED,opposite=null,oldState=0,newState=0] on frame0
java.awt.focus.DefaultKeyboardFocusManager: sun.awt.TimedWindowEvent[WINDOW_GAINED_FOCUS,opposite=javax.swing.JFrame[frame0,752,469,97x80,layout=java.awt.BorderLayout,title=LoggerTest,resizable,normal,defaultCloseOperation=EXIT_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,22,97x58,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true],oldState=0,newState=0] on frame0
java.awt.focus.DefaultKeyboardFocusManager: java.awt.FocusEvent[FOCUS_GAINED,permanent,opposite=null,cause=ACTIVATION] on javax.swing.JButton[,0,0,97x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@56e325b9,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Button 1,defaultCapable=true]
java.awt.focus.DefaultKeyboardFocusManager: 在{0}之前的标记 FOCUS_GAINED
java.awt.focus.DefaultKeyboardFocusManager: >>> 标记转储,时间:{0}
java.awt.focus.DefaultKeyboardFocusManager:     {0}
java.awt.focus.DefaultKeyboardFocusManager: 在FOCUS_GAINED后的标记
java.awt.focus.DefaultKeyboardFocusManager: >>> 标记转储,时间:{0}
java.awt.focus.Component: java.awt.FocusEvent[FOCUS_GAINED,permanent,opposite=null,cause=ACTIVATION] on javax.swing.JButton[,0,0,97x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@56e325b9,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Button 1,defaultCapable=true]
java.awt.focus.DefaultKeyboardFocusManager: java.awt.event.WindowEvent[WINDOW_CLOSING,opposite=null,oldState=0,newState=0] on frame0

代码:

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.logging.ConsoleHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;

/**
 * @see https://dev59.com/QYzda4cB1Zd3GeqPpapp#31223145
 * @see https://dev59.com/IWIi5IYBdhLWcg3w7wDP
 */
public class LoggerTest {

    private void display() {
        JFrame f = new JFrame("LoggerTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLayout(new GridLayout(0, 1));
        f.add(new JButton("Button 1"));
        f.add(new JButton("Button 2"));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        Logger rootLogger = Logger.getLogger("");
        rootLogger.setLevel(Level.ALL);
        logClass("java.awt.focus.Component");
        logClass("java.awt.focus.DefaultKeyboardFocusManager");
        EventQueue.invokeLater(new LoggerTest()::display);
    }

    private static void logClass(String name) {
        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.ALL);
        consoleHandler.setFormatter(new Formatter() {
            @Override
            public String format(LogRecord record) {
                return name + ": " + record.getMessage() + '\n';
            }
        });
        Logger logger = Logger.getLogger(name);
        logger.setLevel(Level.ALL);
        logger.addHandler(consoleHandler);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接