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

Dennis Kriechel picture Dennis Kriechel · Dec 8, 2015 · Viewed 7.2k times · Source

I've got a simple setup to log 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 would expect to get an output on the stdout, just like using System.out.println();.
But instead it gets printed out on the stderr, which results in a red font on the eclipse console:

enter image description here

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

A logger should use stdout for fine+info and use stderr for severe level.

Answer

jmehrens picture jmehrens · Dec 8, 2015

The java.util.logging API was developed under JSR 47: Logging API Specification. According to the change log in the "Proposed Final Draft" the ConsoleHandler always used System.err. The JCP page also lists the original authors of the API and I think only those names truly know the answer to your question.

That said, I think the origin comes from System.err API docs.

Typically this stream corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variable out, has been redirected to a file or other destination that is typically not continuously monitored.

Opposed to System.out:

The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user.

Logging maps to diagnostics and not raw data. It is important to separate data from diagnostic error information especially when piping processes together as the downstream consumers are only ready to accept data information and not error messages. See Confused about stdin, stdout and stderr? for more detailed information.