The moose likes Java in General and the fly likes direct System.out to file AND console Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "direct System.out to file AND console" Watch "direct System.out to file AND console" New topic
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
    
  19

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
    
  19

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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: direct System.out to file AND console
 
Similar Threads
how this setOut method works.......
Good design?
I/O Redirecting problem
tricky program
Is there any newsletter for scjp?