You can redirect System.out
to a custom PrintStream
that throws an error or logs it in a file. You'll have to manually go through the stacktrace and look for the offending method.
To create it:
PrintWriter pw = ...;
PrintStream ps = new PrintStream(System.out) {
public void write(int b) {
super.write(b);
/*try { throw new Error(); }
catch (Error e) { e.printStackTrace(pw); }*/
//Edit: Jeff Wang's answer is a better approach since it doesn't
// throw an exception and just prints the stacktrace
new Exception().printStackTrace(pw);
//You could also write to stderr
Thread.dumpStack();
}
};
To change System.out
:
System.setOut(ps);
If you set this at the start of your program, this'll throw an error whenever a library prints to System.out
, catch it, and write to a file (or somewhere else). You can always format it better if you want.
This isn't a great solution beyond testing, because it involves throwing an exception every time you write a single byte (you could override the other write
method if you wish, though) and because you need to manually go through the stacktraces. In production, you'd want something else.
Edit: Throwing an exception is unnecessary. You can do Thread.dumpStack
or new Exception().printStackTrace
, as the other answer by Jeff Wang suggested.