Search code examples
javastderr

In Java, is there a way to get a callback every time something is written to stderr?


I would like to have my own java code run when anything is written to stderr, but can't seem to find a way to do this generically. The goal is to either receive a callback or override something that prints to the error console perhaps where anytime an error is written such as with log's error function or Exception's printStackTrace, my code would be able to intercept the text. How might this be possible in Java?


Solution

  • You can call System.setErr(PrintStream err) and set it to a PrintStream that does whatever you like. A Throwable will use System.err when printing its stack trace, so your PrintStream would get used.