| Author |
direct System.out to file AND console
|
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
hello,
i was wondering what is the best way to direct all calls on system.out to a file, but still also show the output in the console ...
i came up with the following code, but it looks somehow cumbersome, since you would have to overwrite all implemented mehtods
the implementing code would look like this
is there a better way to achieve this ?
|
JDBCSupport - An easy to use, light-weight JDBC framework -
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
i came up with the following code, but it looks somehow cumbersome, since you would have to overwrite all implemented mehtods
Yea, printstream has a lot of methods... it may be better to write a multi output stream that overrides filter output stream.
Keep in mind that system out is, by default, just a printstream around a file output stream with the STDOUT file descriptor. So, instead of a multi printstream that contains the original printstream and another print stream around a file output stream, a better option would be a print stream around a multi output stream that contains the file output stream of stdout and file output stream of the file.
Something like this...
Only 5 methods to override...
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
hey,
i hope i got that right ...
the general idea of the implementation is correct but instead of overriding PrintStream I override FilterOutputStream to have less methods to overwrite ?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Sebastian Janisch wrote:hey,
i hope i got that right ...
the general idea of the implementation is correct but instead of overriding PrintStream I override FilterOutputStream to have less methods to overwrite ?
Yup... And BTW, you can't assign to the System.out variable -- it is final and can't be reassigned. You have to use the setOut() method instead (to reassign the variable).
Henry
|
 |
Sebastian Janisch
Ranch Hand
Joined: Feb 23, 2009
Posts: 1183
|
|
true, i forgot about that part ...
okay cool thank you
|
 |
 |
|
|
subject: direct System.out to file AND console
|
|
|