I find myself creating this code over and over again, so I decided to create a static class that can do it for you. Basically, it lets you pass either a string or an exception to a method that prints this to an error log. It’s very useful in almost all games as a means of reporting debug information and errors without flooding the screen, and giving the user the ability to send the information to you. I’ll probably make something that sends error reports automatically as well, but I haven’t made one yet so I’m not posting it, obviously. [EDIT]Made it better according to suggestions.[/EDIT]
Anyway, I hope you guys find this useful.
import java.io.PrintWriter;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class ErrorLogger
{
public static void log(Exception e)
{
try
{
PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt",true)));
e.printStackTrace(writer);
writer.close();
}
catch (Exception ex) {}
}
public static void log(String e)
{
try
{
PrintWriter writer = new PrintWriter(new BufferedOutputStream(new FileOutputStream("log.txt",true)));
writer.println(e);
writer.close();
}
catch (Exception ex) {}
}
}