Thursday, June 16, 2011

Dump console output to a file

When a eclipse project using different frameworks like Hibernate, Spring etc etc. a lot of debug log message has been generated by these frameworks. But due to the size limit of console buffer size these log messages are overwritten. One simple solution is to increase the console buffer size but it is usually not recommended because it definitely takes more memory and effect the overall eclipse responsiveness.

Secondly, the more effective way is to keep all the debug log message of the console in a file for back trace. The easiest way is to redirect the eclipse console output to a file. For this do the following settings in eclipse:

Run (menu)-> Run Configuration -> Common (tab) -> (under) Standard Input and Output -> Check File and provide the path for the log file.

Also in some specific scenario when you want to dump the output of your program to a file another way is to set the output stream of your program like this:


OutputStream os = new FileOutputStream("C:\\test\\log.txt");
System.setOut( new PrintStream(os));
System.setErr( new PrintStream(os));
System.out.println("test message");
System.err.println("error message");



OutputStream os = new FileOutputStream("C:\\test\\log.txt");           
System.setOut( new PrintStream(os));
System.setErr( new PrintStream(os));
System.out.println("test message");
System.err.println("error message");

No comments:

Post a Comment