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:
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.
The package consists of a set of classes and interfaces which are used in logging. The System uses a Logger object to log messages. The Logger object is allocated with a LogRecord object which stores the message to be logged. This LogRecord object is forwarded to all the handlers assigned to the Logger object.
There are the five logging handlers in Java: StreamHandler: It writes the formatted log message to an OutputStream. ConsoleHandler: It writes all the formatted log messages to the console. FileHandler: It writes the log message either to a single file or a group of rotating log files in the XML format.
One of the most popular solutions for the Java world is the Apache Log4j 2 framework. Maintained by the Apache Foundation, Log4j 2 is an improvement on the original Log4j, which was the most popular logging framework in Java for many years.
Java Logging Handlers We can add multiple handlers to a java logger and whenever we log any message, every handler will process it accordingly. There are two default handlers provided by Java Logging API. FileHandler: This handler writes all the logging messages to file in the XML format.
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.
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