【发布时间】:2021-11-13 15:58:40
【问题描述】:
我正在尝试在我的 java awt swing 应用程序(它是一个 .jar 应用程序)中实现 java.util.logging。另外,我想记录多个 java 类文件。
因此,我将日志记录功能创建为实用程序类。
请找到我的MyLog 它是一个实用程序类
public class MyLog {
private static MyLog instance = new MyLog();
public static MyLog getInstance() {
return instance;
}
public void info(String msg) {
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler("f://MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info(msg);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
请找到我的实现类。
class AEvent extends Frame implements ActionListener {
private static final long serialVersionUID = 1L;
private MyLog logProp = MyLog.getInstance();
TextField tf;
AEvent() {
// create components
tf = new TextField();
tf.setBounds(60, 50, 170, 20);
Button b = new Button("click me");
b.setBounds(100, 120, 80, 30);
// register listener
b.addActionListener(this);// passing current instance
// add components and set size, layout and visibility
add(b);
add(tf);
setSize(300, 300);
setLayout(null);
setVisible(true);
logProp.info("My first logging message 1");
}
public void actionPerformed(ActionEvent e) {
logProp.info("My first logging message 2");
tf.setText("Welcome");
}
public static void main(String args[]) throws SecurityException, IOException {
new AEvent();
}
}
这里的问题是,日志写入多个文件(意味着生成多个MyLogFile.log),MyLogFile.log.1.lck 这种类型的文件也生成。
请在下面找到日志文件列表。
【问题讨论】:
标签: java logging java.util.logging