Copy STDOUT to file without stopping screen display

The program that I create is designed to run unattended, because of this I redirected the stdout and stderr streams to the log file. Although this works without any problems, while I am still doing and debugging the software that I would like to display on the screen. Is it possible?

To redirect threads I used

System.setErr(logWriter);
System.setOut(logWriter);

Thank.

+4
source share
7 answers

a little rude, maybe, but you can try the following:

private static final isDebugMode = true;

...

if (!isDebugMode) {
  System.setErr(logWriter);
  System.setOut(logWriter);
} 

PrintStream, , . , , , , , , , .

+4

. (.. Log4j) , isDebugMode , "tee" .

import java.io.PrintStream;
import java.io.File;
public class TeeStream extends PrintStream {
    PrintStream out;
    public TeeStream(PrintStream out1, PrintStream out2) {
        super(out1);
        this.out = out2;
    }
    public void write(byte buf[], int off, int len) {
        try {
            super.write(buf, off, len);
            out.write(buf, off, len);
        } catch (Exception e) {
        }
    }
    public void flush() {
        super.flush();
        out.flush();
    }
}

http://www.exampledepot.com/egs/java.lang/Redirect.html

// import java.io.FileOutputStream;
String dateString = new SimpleDateFormat("yyyyMMdd").format(new Date());
File logFile = new File("mylogfile_" + dateString +".log");
PrintStream logOut = new PrintStream(new FileOutputStream(logFile, true));

PrintStream teeStdOut = new TeeStream(System.out, logOut);
PrintStream teeStdErr = new TeeStream(System.err, logOut);

System.setOut(teeStdOut);
System.setErr(teeStdErr);

LOG.somelevel(msg) , System.out.println(msg). , , .

+8

Unix- ( Windows), tee:

java myprogram | tee output

, output.

+4

, . :

  • . Linux/Unix less "follow" -mode (Shift-F), . . , .
  • (java.util.logging, Log4j ). . ( ), .
+2

UNIX, tail -f logfile ,

+1

Unix -

.. exex exe > log stdout Exe .

, ,

0

Linux Windows ( cygwin) log4j , "" . -f ( -F) , . , , , . , :

( cd /var/log/myapp/; tail -Fq --lines=0 -s 0.05 $(find . -type f -name "*$(date '+%Y-%m-%d').log" ) ) &

/var/log/myapp/, . log4j. -s 0,05 0,05 .

0

All Articles