为什么我的Java记录器文件不起作用?

时间:2014-05-12 14:29:06

标签: java

我在本例中使用Java内置的记录器。它写入我的日志文件,但现在不是。

问题:为什么我的档案没有被写入?

我在这里粘贴了所有代码,这样如果有人想尝试它,那将是一个简单的复制和粘贴。但记录器行在构造函数中。

代码:

import java.io.*;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class FileChooserDemo extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    static private final String newline = "\n";
    JButton openButton;
    JButton saveButton;
    JTextArea log;
    JFileChooser fc;

    private static Logger logger = Logger.getLogger(FileChooserDemo.class.getName());
    FileHandler fh;   

    public FileChooserDemo() {
        super(new BorderLayout());

        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
            fh = new FileHandler("C:\\Users\\itpr13266\\Desktop\\MyNewLogFile.log");   
            logger.info("Logging an INFO-level message");
            logger.warning("Your file cannot be written!!");
        } catch (Exception ex) {
            Logger.getLogger(FileChooserDemo.class.getName()).log(Level.SEVERE, null, ex);
        }

        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);

        fc = new JFileChooser();

        openButton = new JButton("Open a File...");
        openButton.addActionListener(this);

        saveButton = new JButton("Save a File...");
        saveButton.addActionListener(this);

        JPanel buttonPanel = new JPanel(); //use FlowLayout
        buttonPanel.add(openButton);
        buttonPanel.add(saveButton);

        add(buttonPanel, BorderLayout.PAGE_START);
        add(logScrollPane, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == openButton) {
            int returnVal = fc.showOpenDialog(FileChooserDemo.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                log.append("Opening: " + file.getName() + "." + newline);
            } else {
                log.append("Open command cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());
        } else if (e.getSource() == saveButton) {
            int returnVal = fc.showSaveDialog(FileChooserDemo.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                log.append("Saving: " + file.getName() + "." + newline);
            } else {
                log.append("Save command cancelled by user." + newline);
            }
            log.setCaretPosition(log.getDocument().getLength());
        }
    }

    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = FileChooserDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("FileChooserDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new FileChooserDemo());

        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        createAndShowGUI();
     }
}

记录打印输出:

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2014-05-12T10:30:00</date>
  <millis>1399908600500</millis>
  <sequence>0</sequence>
  <logger>TestMenu.FileChooserDemo</logger>
  <level>INFO</level>
  <class>TestMenu.FileChooserDemo</class>
  <method>&lt;init&gt;</method>
  <thread>1</thread>
  <message>Logging an INFO-level message</message>
</record>
<record>
  <date>2014-05-12T10:30:00</date>
  <millis>1399908600517</millis>
  <sequence>1</sequence>
  <logger>TestMenu.FileChooserDemo</logger>
  <level>WARNING</level>
  <class>TestMenu.FileChooserDemo</class>
  <method>&lt;init&gt;</method>
  <thread>1</thread>
  <message>Your file cannot be written!!</message>
</record>
</log>

1 个答案:

答案 0 :(得分:1)

我不相信您要将自己创建的FileHandler fh添加到Logger logger

logger.addHandler(fh);

您的日志可能正在写入,但不在您尝试指定的位置。

fh = new FileHandler("C:\\Users\\itpr13266\\Desktop\\MyNewLogFile.log");   
logger.addHandler(fh);
logger.info("Logging an INFO-level message");
logger.warning("Your file cannot be written!!");