java swing 日志_启用Java Swing日志记录(密钥发送)

正如建议

here,我不确定使用sun.util.logging.PlatformLogger是否合适.要记录焦点事件,我必须在根记录器上指定更高级别,例如Level.ALL.添加ConsoleHandler(如

this)可以使日志更易于阅读.

安慰:

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: focus owner is null or this

java.awt.focus.DefaultKeyboardFocusManager: Enqueue at {0} for {1}

java.awt.focus.Component: Pass for 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: Markers before FOCUS_GAINED on {0}

java.awt.focus.DefaultKeyboardFocusManager: >>> Markers dump, time: {0}

java.awt.focus.DefaultKeyboardFocusManager: {0}

java.awt.focus.DefaultKeyboardFocusManager: Markers after FOCUS_GAINED

java.awt.focus.DefaultKeyboardFocusManager: >>> Markers dump, time: {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://stackoverflow.com/a/31223145/230513

* @see https://stackoverflow.com/q/20815048/230513

*/

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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值