简体   繁体   中英

Do action when something is printed in System.err

Like in topic - I am trying to do action when anything is printed into error stream, but code below is not working:

try {
    System.setOut(new PrintStream("InfoLog.txt"));
    System.setErr(new PrintStream(new FileOutputStream("ErrorLog.txt") {
        public void write(int b) throws IOException {
            super.write(b);
            error();
        }
        public void write(byte[] b) throws IOException {
            super.write(b);
            error();
        }
    }));
} catch (FileNotFoundException ex) {
    Logger.getLogger(HouseCalc.class.getName()).log(Level.SEVERE, null, ex);
}

Is there any way to "catch" the System.err stream and make any action before it gets printed to file?

Make sure to override the appropriate methods . Have a good look at the source of PrintStream , because they might be calling each other . You want to get all the way down.

write(char cbuf[], int off, int len)

might be the one you really need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM