Write all System.out to file

I have a great program in which I used System.out for debugging. But I would like to be able to register all System.out simultaneously with the file, so even if my program is not running in the console, all System.out will be written to the file, and I can see it at any time when the program is running or later.

Btw, it is impractical to scan the entire program and use the recorder instead of the System.out!

I tried java -jar myjar.jar > sysout.txt, but it does not log exceptions recorded by the java logger utility.

+4
source share
2 answers

How about System.setOut(PrintStream)? You can insert this call into the initialization part of your program (start).

There used to be a commment:

And of course, you can do the same with System.err - namely System.setErr (PrintStream), but better for a different file stream.

Details of the proposed autofill adding and buffering stream:

    String file = ...;
    PrintStream ps = 
      new PrintStream(true, new BufferedOutputStream(new FileOutputStream(file, true)));
    System.setOut(ps);
+5
source

Normal redirection command_name args > outputredirects only standard output. It does not redirect the standard error stream, where errors are usually logged. To output both streams to the same file,

command args &> filename

eg. java -jar myjar.jar &> sysout.txt

To redirect to different files, use

command args 1> output_file 2> error_file

eg. java -jar myjar.jar 1> sysout.txt 2> errout.txt

See additional options http://www.tldp.org/LDP/abs/html/io-redirection.html

+4
source

All Articles