You will have to use one (or more) of the stream classes to create a stream that outputs to a file. I think there is a method to set System.err to refer to the stream you create, but you will have to check the API to find out for sure. Layne
There are three basic approaches I can think of: (1) Use OS-level output redirection, e.g. java MyClass 1> output.log 2> error.log or java MyClass 1> all_output.log 2>&1 I believe the same syntax works in Unix and Windows here. This is easy, and requires no changes inside your code. (2) Use System.setOut() and System.setErr() within your program to redirect the output to a stream writing to a file, as Layne suggests. Also pretty easy. (3) Replace the various println() statements with more advanced logging techniques, using classes such as those in java.util.logging or the Log4J package. This is more work than the other two, but may be more rewarding as it will allow you much more flexibility to make other adjustments later. E.g. you may initially decide that loggers will simply write messages to the screen. Later you can easily revise this to write all messages to one detailed log file, and also write only errors and important non-error messages to a different file, and also create a pop-up window to inform the user of errors. (For example.) These later changes can be made by making changes just to the code that creates and configures the loggers (or maybe just to configuration files which are read by your code). Most of the code in the other classes that do the logging will not need to be changed at all. I like the third option for a big project, where the power and flexibility I get later makes it worthwhile to spend a little extra energy at teh beginning to set up logging. For smaller projects, the first two options may be appropriate.
"I'm not back." - Bill Harding, Twister
posted 17 years ago
Hi!, ThanQ!!!Jim. Regards. Vijaya.
I can't take it! You are too smart for me! Here is the tiny ad: