在Swing应用程序中使用java.util.logging包 [英] Using the java.util.logging package in a Swing application
问题描述
此问题与在Java应用程序中实现日志记录有关,但由于那里的示例代码使问题变得难以置信的漫长,因此我决定根据到目前为止所学的知识开始一个新的问题.
This question is related to Implementing logging in a Java application, but since the example code there has made the question incredibly long, I decided to start a new question based on what I have learned so far.
为了说明我要做什么,我有以下示例类扩展了JFrame:
To illustrate what I am trying to do, I have the following example class which extends JFrame:
package swingloggingsscce;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class SwingLoggingSSCCE extends javax.swing.JFrame {
public SwingLoggingSSCCE() {
initComponents();
}
private void initComponents() {
logButton = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Swing Logging SSCCE");
logButton.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
logButton.setText("Do Log");
logButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
logButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(147, 147, 147)
.addComponent(logButton)
.addContainerGap(146, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(120, 120, 120)
.addComponent(logButton)
.addContainerGap(143, Short.MAX_VALUE))
);
pack();
}
private void logButtonActionPerformed(java.awt.event.ActionEvent evt) {
Logger.getLogger(SwingLoggingSSCCE.class.getName()).log(Level.INFO, "SwingLoggingFrame.logButtonActionPerformed()");
}
public static void main(String args[]) throws IOException {
SwingLoggingSSCCE.initLogger();
new SwingLoggingSSCCE().setVisible(true);
}
private static void initLogger() throws IOException {
SwingLoggingSSCCE.HANDLER = new FileHandler(SwingLoggingSSCCE.LOG_FILE_NAME);
SwingLoggingSSCCE.HANDLER.setFormatter(new SimpleFormatter());
Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);
logger.addHandler(SwingLoggingSSCCE.HANDLER);
}
private javax.swing.JButton logButton;
private static final String LOG_FILE_NAME = "swingloggingsscce.log";
private static FileHandler HANDLER = null;
}
这完全符合预期,并生成以下"swingloggingsscce.log"文件:
This works exactly as expected and produces the following "swingloggingsscce.log" file:
Sep 09, 2012 8:37:43 PM swingloggingsscce.SwingLoggingSSCCE logButtonActionPerformed
INFO: SwingLoggingFrame.logButtonActionPerformed()
现在,我试图在主应用程序中使用相同的功能.这是带有main()
的类:
Now I am trying to get the same thing to work in my main application. This is the class with main()
:
/*
* This file is part of BBCT.
*
* Copyright 2012 codeguru <codeguru@users.sourceforge.net>
*
* BBCT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BBCT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package bbct;
import bbct.data.BaseballCardIO;
import bbct.data.BaseballCardJDBCIO;
import bbct.exceptions.BBCTIOException;
import bbct.gui.BBCTFrame;
import bbct.gui.GUIResources;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.swing.JOptionPane;
/**
* This is the driver class for the Baseball Card Tracker program.
*
* @author codeguru <codeguru@users.sourceforge.net>
*/
public class Baseball {
private static final String LOG_FILE_NAME = "log/bbct.log";
/**
* Starts the Baseball Card Tracker by creating and showing the initial
* window.
*
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
Baseball.initLogger();
// ****************** Added this line ******************
Logger.getLogger(Baseball.class.getName()).log(Level.INFO, "Fixing to create a BaseballCardIO object.");
BaseballCardIO bcio = new BaseballCardJDBCIO(GUIResources.DB_URL);
Logger.getLogger(Baseball.class.getName()).log(Level.INFO, "Fixing to show a new frame.");
new BBCTFrame(bcio).setVisible(true);
} catch (BBCTIOException | IOException ex) {
Logger.getLogger(Baseball.class.getName()).log(Level.SEVERE, "Unable to initialize storage.", ex);
JOptionPane.showMessageDialog(null, ex.getMessage(), "Initialization Error", JOptionPane.ERROR_MESSAGE);
}
}
private static void initLogger() throws IOException {
boolean append = true;
Handler handler = new FileHandler(Baseball.LOG_FILE_NAME, append);
handler.setFormatter(new SimpleFormatter());
Logger logger = Logger.getLogger("");
logger.setLevel(Level.INFO);
logger.addHandler(handler);
}
private static final String LOG_FILE_NAME = "log/bbct.log";
}
由于完整的应用程序有24个类,因此我将不包括其余的代码.问题是我的BBCT应用程序创建了一个"log/bbct.log"文件,但是运行该应用程序后,该文件为空!我没有看到Baseball
类的代码与SwingLoggingSSCCE
有什么不同.显然,有些东西是不同的,否则它将起作用.我认为,我只需要一些新鲜的眼睛即可查看我的代码.同时,我将尝试自行解决.
I won't include the rest of the code as there are 24 classes for the complete application. The problem is that my BBCT application creates a "log/bbct.log" file, but after running the application, the file is empty! I don't see how my code for the Baseball
class differs from SwingLoggingSSCCE
. Obviously something is different, though, or else it would work. I just need some fresh eyes to look at my code, I think. In the mean time, I'll try to figure it out on my own.
预先感谢您的帮助.
我忘了提到SwingLoggingSSCCE
还会在控制台上显示日志记录信息,但是bbct.Baseball
不会.
I forgot to mention that SwingLoggingSSCCE
also displays the logging information on the console, but bbct.Baseball
does not.
更新:
好的,我已经将问题缩小了一点.在创建BaseballCardIO
对象之前,我添加了对Logger.log()的调用.这会出现在日志文件中,但不会出现在创建bcio
之后的文件中.我想我只需要从那里继续进行调查.
Update:
Okay, I have narrowed down the problem a little bit. I added a call to Logger.log() before creating the BaseballCardIO
object. This appears in the log file, but not the one after bcio
is created. I guess I just need to continue my investigation from there.
**另一个更新:**
** Another Update:**
以下构造函数似乎是我的日志记录问题的来源:
The following constructor seems to be the source of my logging issues:
public BaseballCardJDBCIO(String url) throws BBCTIOException {
try {
Logger logger = Logger.getLogger(BaseballCardJDBCIO.class.getName());
logger.log(Level.INFO, "Creating BaseballCardJDBCIO object");
logger.log(Level.INFO, "Getting database connection.");
this.conn = DriverManager.getConnection(url);
logger.log(Level.INFO, "Creating table");
this.createTable();
} catch (SQLException ex) {
// TODO: Need a more user-friendly error message.
throw new BBCTIOException(ex);
}
}
在调用DriverManager.getConnection(url);
之后,所有日志记录都会停止.一些进一步的研究表明,JDBC使用java.util.logging.我可能不希望所有的JDBC日志记录数据.但是,JDBC似乎干扰了我需要添加到应用程序中的日志记录.这是JDBC的功能"吗?
All logging stops after the call to DriverManager.getConnection(url);
. Some further research shows that JDBC uses java.util.logging. I probably don't want all of the JDBC logging data. However, JDBC seems to be interfering with the logging I need to add to my application. Is this a "feature" of JDBC?
推荐答案
我发现了问题,并发布了新的问答,以期(希望)更简洁地说明并给出解决方案:
I found the problem and posted a new Q&A to (hopefully) explain more concisely and give my solution: Using java.util.logging with JDBC drivers for the HyperSQL Database Engine
这篇关于在Swing应用程序中使用java.util.logging包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!