In my project test suite there is big usage of
System.out.println
I'm trying to redirect these output to log file (through configuration or from single point without refactoring whole project ) so that can be disabled when necessary to improve performance. I'm using log4j for logging. Does any one know is this possible ? if so how to do it ? Thanks in advance.
Given that it's better replace the
System.out.println()
, sometimes we have no choice. Anyway I've made a little utility for that:Then all the println originated from MyClass will be redirected to the logger. See this post for more details...
I think you can use
System.setOut(PrintStream)
to set your output to a file output stream. Then you can put this line in your BeforeClass method. I like to use a BaseTest class and put this line of code in the beforeclass method of that class. Then make all test cases extend this cclass.My suggestion would be to refactor if possible. For a possible solution, check these similar questions
log4j redirect stdout to DailyRollingFileAppender
Redirect System.out.println to Log4J, while keeping class name information
Use shell redirection. Figure out the "java" invocation for your project, if you're on most vaguely UNIX-like systems, ps aux | grep java will help.
Then just run this command with > /path/to/logfile. Example: