0

I am using a proprietary API in Java. Its connect method unleashes a barrage of log messages on my standard output. That makes it impossible for me to read the messages from my own logger. Unfortunately, the setOut method of the API does not work.

Is there a way to block the connect method from writing to standard output?

Elias Strehle
  • 1,722
  • 1
  • 21
  • 34
  • 1
    Maybe I didn't understand the question but you could set "System.out" to a dummy output file before calling the connect method. – Robert Kock Mar 13 '18 at 14:38
  • If it uses `System.out`, then `System.setOut` should work. If it uses some other means (e.g. JNI and writing to stdout) then it's complicated. In C, you would do something like that: `oldout = dup(stdout); close(stdout); open("/dev/null"); ...; close(stdout); dup(oldout); close(oldout);` – Johannes Kuhn Mar 13 '18 at 14:39
  • Find out what logger the API is using and lower its log level. Complain to the vendor. – user207421 Mar 13 '18 at 15:19

1 Answers1

1

You can use System.setOut(new PrintStream(new ByteArrayOutputStream())); (you may or may not want to save the reference to the ByteArrayOutputStream) before calling the appropriate method, since you can't modify the method itself, and System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); after calling it

drilow
  • 412
  • 2
  • 10
  • I created a [Null OutputStream](https://stackoverflow.com/questions/691813/is-there-a-null-outputstream-in-java) and set `System.setOut(new PrintStream(new NullOutputStream()));`. That blocks everything I send via `System.out.println("...");` but not the API output (nor my logger's output, strangely). – Elias Strehle Mar 13 '18 at 15:11