Why does java.util.logging.Logger print to stderr?

I have a simple setup for registering a message: JDK 8 Update 65 and Eclipse Mars

 import java.util.logging.Logger; public class Example { private final static Logger LOGGER = Logger.getLogger(Example.class.getName()); public static void main(String[] args) { LOGGER.info("Test"); } } 

I expect to get output to stdout , as with System.out.println(); .
But instead, it prints to stderr , which results in a red font on the eclipse console:

enter image description here

I know I can change this behavior by writing a custom Handler , but I want to know why the default output appears on stderr instead of stdout ?

The registrar should use stdout for fine + info and use stderr for severe .

+9
source share
5 answers

By default, the logger outputs log entries of INFO level and higher (i.e. INFO, WARNING and SEVERE) to the standard error stream (System.err).

Source: www3.ntu.edu.sg/home/ehchua/programming/java/JavaLogging.html

+1
source

This is well documented. By default, registrars publish their parent handlers recursively to the tree until another handler is specified. You can iterate over the parent handlers and see that the default handler for the parent logger is the ConsoleHandler, which uses System.err to publish log entries.

 public class Main { public static void main(String[] args) { Handler[] handlers = Logger.getLogger(Main.class.getName()).getParent().getHandlers(); for (Handler handler : handlers) { System.out.println(handler.getClass().getName()); } } } 
+3
source

The java.util.logging API was developed under JSR 47: Logging API Specification . According to the changelog in the "Proposed Final Project" ConsoleHandler always used System.err. The original authors of the API are also listed on the JCP page, and I think that only those names really know the answer to your question.

I would suggest that the origin comes from the thinking that is logged for the crash report, and the crash reports go to System.err. Indeed, for ConsoleHandler, any level higher than INFO should go to the error stream, and any INFO level or less should go to the stream if you are trying to preserve the JDK use cases before adding the logging API.

+3
source

the reason they use stderr is because this thread is "used for error messages and diagnostics issued by the program" (source: https://www.gnu.org/software/libc/manual/html_node/Standard- Streams.html ).

0
source

You can create and extend ConsoleHandler to install System.out instead of System.err

 Logger logger = Logger.getLoger("your_logger_name"); logger.setUseParentHandler(false); logger.addHandler(new ConsoleHandler() { {setOutputStream(System.out);} }); 

Now all messages from this registrar will appear in the System.out console.

0
source

All Articles