在Swing应用程序中使用java.util.logging包

在Swing应用程序中使用java.util.logging包,java,swing,logging,java.util.logging,Java,Swing,Logging,Java.util.logging,这个问题与相关,但由于示例代码让问题变得非常长,因此我决定根据迄今为止所学的内容开始一个新问题 为了说明我试图做什么,我有以下扩展JFrame的示例类: package swingloggingsscce; import java.io.IOException; import java.util.logging.FileHandler; import java.util.logging.Level; import java.util.logging.Logger; import java.ut

这个问题与相关,但由于示例代码让问题变得非常长,因此我决定根据迄今为止所学的内容开始一个新问题

为了说明我试图做什么,我有以下扩展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”文件:

现在,我正试图在我的主应用程序中实现同样的功能。这是带有
main()
的类:


调用
DriverManager.getConnection(url)后,所有日志记录都会停止。一些进一步的研究表明,JDBC使用java.util.logging。我可能不想要所有的JDBC日志数据。然而,JDBC似乎干扰了我需要添加到应用程序中的日志记录。这是JDBC的一个“功能”吗?

我发现了问题并发布了一个新的Q&a(希望)更简洁地解释并给出我的解决方案:

我发现了问题并发布了一个新的Q&a(希望)更简洁地解释并给出我的解决方案:

在你(或我们)解决问题之前,你必须先隔离问题。如果可能,您应该单独测试每个类,必要时使用模拟类。@HovercraftFullOfEels谢谢您的建议。我现在正在努力解决这个问题,并将问题缩小了一点。@HovercraftFullOfEels嘿,我将问题缩小到一个JDBC调用。知道下一步该怎么做吗?除非JDBC调用引发异常,否则我看不出为什么不应该点击“创建表”记录器。还要在异常块中添加日志语句。在您(或我们)解决问题之前,必须先隔离问题。如果可能,您应该单独测试每个类,必要时使用模拟类。@HovercraftFullOfEels谢谢您的建议。我现在正在努力解决这个问题,并将问题缩小了一点。@HovercraftFullOfEels嘿,我将问题缩小到一个JDBC调用。知道下一步该怎么做吗?除非JDBC调用抛出异常,否则我看不出为什么不应该点击“Creating table”记录器。在异常块中也添加一条log语句。
Sep 09, 2012 8:37:43 PM swingloggingsscce.SwingLoggingSSCCE logButtonActionPerformed
INFO: SwingLoggingFrame.logButtonActionPerformed()
/*
 * 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";
}
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);
    }
}