our project uses System.out.println() or System.err.println() to output debugging message. Now we want to use log4j to direct these output to log files not just console. To avoid impact or risk, we don't want to do replace all "System.out.println()" with "logger.debug" but just want to maybe write several code to make these output redirect to log4j log files that is no code change in each class still using System.out.println() (we know this is bad practice but we don't want to do global search and replace currently). Is it possible? Thanks.
I guess it is possible, though it is definitely a bad idea. You should bite the bullet and replace all of the System.out.printXXx()
and System.err.printXXx()
calls.
(You don't have to do this in one big change, and you don't have to do it using an automated search / replace ... if these things make you feel uncomfortable.)
You can change where those streams go by using System.setOut
and System.setErr
to point them to a log4j log file. However:
A more complicated alternative would to create special OutputStream object to intercept the output and turn it into calls to a log4j Logger. But this also would have problems too:
out
and one for err
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With