Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Last active May 10, 2017 19:10
Show Gist options
  • Save LuxXx/a0e17e1fc1e82ada6d6291b9896b8786 to your computer and use it in GitHub Desktop.
Save LuxXx/a0e17e1fc1e82ada6d6291b9896b8786 to your computer and use it in GitHub Desktop.
A Logger maybe for re-use
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
public final class Logger {
private static Logger INSTANCE;
private static String logfile;
private static SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
private boolean debug;
private boolean date;
private Logger() {
logfile = "error.log";
}
public static Logger getInstance() {
if (INSTANCE == null) INSTANCE = new Logger();
return INSTANCE;
}
public void log(String s) {
System.out.println(formatDate(s));
}
public void debug(String s) {
if (debug) log(s);
}
public void err(String s) {
System.err.println(formatDate(s));
}
public void box(String s) {
JOptionPane.showMessageDialog(null, formatDate(s));
}
public void fprint(String s) {
FileWriter writer = null;
try {
writer = new FileWriter(new File(logfile), true);
writer.write(formatDate(s));
writer.write('\n');
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
private String formatDate(String s) {
return date ? new StringBuilder().append('[').append(currentDate()).append(']').append(' ').append(s).toString() : s;
}
private static String currentDate() {
return sdf.format(new Date());
}
public boolean debug() {
debug = !debug;
return debug;
}
public boolean date() {
date = !date;
return date;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment