I'm using the standard ConsoleHandler
from java.util.logging
and by default the console output is directed to the error stream (i.e. System.err
).
How do I change the console output to the output stream (i.e. System.out
)?
I'm using the standard ConsoleHandler
from java.util.logging
and by default the console output is directed to the error stream (i.e. System.err
).
How do I change the console output to the output stream (i.e. System.out
)?
If you use Java logging, you can change the default handler:
For example, for files: Handler fh = new FileHandler(FILENAME); Logger.getLogger(LOGGER_NAME).addHandler(fh);
If you want to output to a stream you can use StreamHandler, I think you can configure it with any output stream that you woud like, including the system stream.
The ConsoleHandler will grab a snapshot of
System.err
during construction. One option would be to swap the global error stream with the global out stream and then create the ConsoleHandler.This assumes that the code has permission to modify error stream and that no other running code is accessing the error stream. In short, this is an option but there are safer alternatives.
Hmm I just got bit in the foot a few times, trying to accomplish this feat. Before googling my way here I managed to conjure the following hack. Ugly, but it seems to get the job done.
Watch out: Calling setOutputStream() from the constructor is tempting, but it does (as Jon Skeet already pointed out) close System.err. Mad skills!
Simply extend StreamHandler & in the constructor call Super(System.out,). This will avoid closing System.err - Thanks
Have a look at the docs and source for ConsoleHandler - I'm sure you could easily write a version which just uses System.err instead of System.out. (It's a shame that ConsoleHandler doesn't allow this to be configured, to be honest.)
Then it's just a case of configuring the logging system to use your new StdoutHandler (or whatever you call it) in the normal way.
If you set setUseParentHandlers(false); only THAT class has it set. Other classes in the app will still pass it thru to stderr.