从多个类登录到文件系统

时间:2014-09-19 08:37:01

标签: java logging io java.util.logging

我在java.util.logging.Logger中使用构建来在运行时记录一些信息。我还想把日志写在文件系统上。所以基本上我以为我会用Singleton Pattern创建Logger,在我的所有类中使用它,只需将文件保存到文件系统。不幸的是,没有写过任何文件。我在控制台输出中看到日志,所有多个类的所有日志都显示在那里就好了。但我不确定为什么我的代码不会创建文件。

这是我尝试过的:

package util;

import gui.ExceptionDialog;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;


    public class LoggerWrapper {

    public static final Logger myLogger = Logger.getLogger("Stockparty Log");

    private static LoggerWrapper instance = null;
    private static Date date = new Date();
    private static SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
    private static String fileName = "spLog-"+formatter.format(date)+".log";

    public static LoggerWrapper getInstance() {
        if(instance == null) {
            prepareLogger(Level.INFO);
            instance = new LoggerWrapper ();
        }
        return instance;
    }


    private static void prepareLogger(Level level) {

        File file = null;
        if (OSValidator.isWindows()) {
            System.out.println("This is Windows");
            file = new File(System.getenv("APPDATA") + File.separator + "StockParty" + File.separator + fileName);

            if (!file.exists()) {
                try {
                    FileUtils.forceMkdir(file.getParentFile());
                } catch (IOException ex) {
                    ExceptionDialog exceptionDialog = new ExceptionDialog("Schreibfehler", "Ordnder, um Statistik abzulegen konnte nicht erzeugt werden", ex);
                    exceptionDialog.show();
                }
            }

        } else if (OSValidator.isMac()) {
            System.out.println("This is Mac");

            file = new File(System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support"
                    + File.separator + "StockParty" + File.separator + fileName);
            if (!file.exists()) {
                try {
                    FileUtils.forceMkdir(file.getParentFile());
                } catch (IOException ex) {
                    ExceptionDialog exceptionDialog = new ExceptionDialog("Schreibfehler", "Ordnder, um Statistik abzulegen konnte nicht erzeugt werden", ex);
                    exceptionDialog.show();
                }
            }

            FileHandler myFileHandler = null;
            try {
                myFileHandler = new FileHandler(file.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            myFileHandler.setFormatter(new SimpleFormatter());
            myLogger.addHandler(myFileHandler);
            myLogger.setUseParentHandlers(false);
            myLogger.setLevel(level);
        }
    }

}

哦,顺便说一句APPDATA中的目录只是文件丢失了。

0 个答案:

没有答案