0

I am currently using java.util.logger in my application. It sends its output to System.err. I need it to continue to do this, but I also need to send that output to something else.

Is there a way to copy the System.err stream so that it can send to two different sources?

user489041
  • 27,916
  • 55
  • 135
  • 204

1 Answers1

5

As always when note people referencing java.util.logger I feel inclined to at least recommend looking at another logging library, such as logback and slf4j.

This blog post describes a method which can easily be adapted to support what you're asking for. In short what you need to do is define a PrintStream which can duplicate its output, assign this using:

System.setErr(doubleLoggingPrintStream)
Johan Sjöberg
  • 47,929
  • 21
  • 130
  • 148
  • Very interesting. I have noticed log4j referenced many times before. Do you suggest one over the other? – user489041 Feb 02 '11 at 22:07
  • No, it's all preference. We tend to use log4j as most of our dependencies are already based on it. If you use [commons-logging](http://commons.apache.org/logging/) your application can transparently switch between log4j/slf4j based on which library can be found in your classpath. – Johan Sjöberg Feb 02 '11 at 22:08
  • The author of log4j recommends logback: http://logback.qos.ch/reasonsToSwitch.html – maaartinus Feb 02 '11 at 22:40