The first of these is a neat bit of Java code that redirects calls to System.out and System.err. So when you use System.out instead of printing to the console it actually prints to a text file, this is great for log files.
First of all you need to add the following variables to your Java program:
private PrintStream logFile;
private PrintStream errFile;
Then add the following function/method:
public void initLogs()
{
// Try to redirect system output to log file
try
{
logFile = new PrintStream(new FileOutputStream("mypath/mylogname.log", true), true);
}
catch (FileNotFoundException e)
{
System.err.println("Error: Could not find log file!");
}
catch (IOException e)
{
System.err.println("Error: Could not open log file!");
e.printStackTrace();
}
// Fail gracefully
if (logFile != null)
{
System.setOut(logFile);
}
// Try to redirect system errors to error log file
try
{
errFile = new PrintStream(new FileOutputStream("mypathname/mylogname.err.log", true), true);
}
catch (FileNotFoundException e)
{
System.err.println("Error: Could not find log file!");
}
catch (IOException e)
{
System.err.println("Error: Could not open log file!");
e.printStackTrace();
}
// Fail gracefully
if (errFile != null)
{
System.setErr(errFile);
}
}
This function is the first thing I call in my application, that way all subsequent calls to System.out and System.err will write to the appropriate log files. The function itself is pretty straight forward.
First of all we try and open a stream to the desired log file, we do this on this line:
logFile = new PrintStream(new FileOutputStream("mypath/mylogname.log", true);
This opens the file specified by "mypath/mylogname.log" and readies it for printing, the true value just tells the function to add the new text to the end of any existing text in this file, perfect for logs, if you want it to wipe the file and start fresh each time just set this flag to false.
Next if we are successful we tell System.out to print to the new file we just opened instead of the console, to do this we call the following:
System.setOut(logFile);
The rest of the code explains itself really, its just simple error checking so that if the program cant open the log files it will fallback to printing to the console and not expload.
NOTE: If you are using this code in an applet you will need to make the applet files into a .jar file and then sign the .jar file with a appropriate security certificate otherwise web browsers may not allow your applet to write files to disk. View this page for details on .jar files
Packaging Programs in JAR Files
I hope this code was of use to someone out there, maybe I will post something more exciting next time... but maybe not
2 comments:
This is a bit boring.
When are we going to find out about your personal life?
wow..!
How about starting with a "Hello World" applet for the web? I'm a little bit behind on the olde Java coding. lol. All my memory of it has been pushed out by other things such as thoughts about Care Bears, Milkshake, and Penny Farthings.
Please Help!
Post a Comment