这个问题与Implementing logging in a Java application有关,但由于示例代码使问题长得令人难以置信,我决定根据我迄今所学到的内容开始一个新问题。
为了说明我想要做的事情,我有以下扩展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"文件:
Sep 09, 2012 8:37:43 PM swingloggingsscce.SwingLoggingSSCCE logButtonActionPerformed
INFO: SwingLoggingFrame.logButtonActionPerformed()
现在我想在我的主应用程序中使用相同的东西。这是包含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应用程序创建了一个&#34; log / bbct.log&#34;文件,但运行应用程序后,文件为空!我不知道Baseball
类的代码与SwingLoggingSSCCE
的不同之处。显然,某些东西是不同的,否则它会起作用。我想,我只需要一些新鲜的眼睛来看我的代码。与此同时,我会尝试自己解决这个问题。
提前感谢您的帮助。
修改
我忘了提及SwingLoggingSSCCE
也在控制台上显示日志信息,但bbct.Baseball
没有。
更新
好的,我已经把问题缩小了一点。在创建BaseballCardIO
对象之前,我添加了对Logger.log()的调用。这将显示在日志文件中,但不会显示在创建bcio
之后的日志文件中。我想我只需要从那里继续我的调查。
**另一个更新:**
以下构造函数似乎是我的日志记录问题的来源:
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似乎在干扰我需要添加到应用程序的日志记录。这是一个&#34;功能&#34; JDBC?
答案 0 :(得分:0)
我发现了问题并发布了一个新的Q&amp; A(希望)更简洁地解释并提供我的解决方案:Using java.util.logging with JDBC drivers for the HyperSQL Database Engine