Pages

Java Log File Writer

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;

/**
 *
 * @author Cy.Co.Labs
 */
public final class LogFile {

    //Implements a singleton logger instance
    private static final LogFile instance = new LogFile();

    //Retrieve the execution directory. Note that this is whereever this process was launched
    public String logname = "errorlog";
    protected String env = System.getProperty("user.dir");
    private static File logFile;

    public static LogFile getInstance() {
        return instance;
    }

    public static LogFile getInstance(String withName) {
        instance.logname = withName;
        instance.createLogFile();
        return instance;
    }

    public void createLogFile() {
        //Determine if a logs directory exists or not.
        File logsFolder = new File(env + '/' + "Logs");
        if (!logsFolder.exists()) {
            //Create the directory
            System.err.println("INFO: Creating new logs directory in " + env);
            logsFolder.mkdir();

        }

        //Get the current date and time
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();

        //Create the name of the file from the path and current time
        logname = logname + '-' + dateFormat.format(cal.getTime()) + ".log";
        LogFile.logFile = new File(logsFolder.getName(), logname);
        try {
            if (logFile.createNewFile()) {
                //New file made
                System.err.println("INFO: Creating new log file");
            }
        } catch (IOException e) {
            System.err.println("ERROR: Cannot create log file");
            System.exit(1);
        }
    }

    private LogFile() {
        if (instance != null) {
            //Prevent Reflection
            throw new IllegalStateException("Cannot instantiate a new singleton instance of log");
        }
        this.createLogFile();
    }

    //public static void log(String message) {
    public static void log(String classname, String method, String error, JComponent component) {
        try {
            FileWriter out = new FileWriter(LogFile.logFile, true);
            out.write("\r\n");
            out.write(classname + "-" + method + "-" + error);
            out.close();
            if (component != null) {
                JOptionPane.showMessageDialog(component.getRootPane(), error, "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(null, error, "Error", JOptionPane.ERROR_MESSAGE);
            }
        } catch (IOException e) {
            System.err.println("ERROR: Could not write to log file");
            JOptionPane.showMessageDialog(component.getRootPane(), "ERROR: Could not write to log file.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static void main(String[] args) {
        //LogFile.log("This is a message");
    }
}

To Use:

try {
        } catch (Exception e) {
            e.printStackTrace();
            LogFile.log(getClass().getName(), "getExtraCommisToTable", e.getMessage().toString(), this.getRootPane());
        }

No comments:

Post a Comment