我有一个简单的设置来记录消息:JDK 8 Update 65
和 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");
}
}
我希望在 stdout
上得到输出, 就像使用 System.out.println();
.
而是它被打印在 stderr
上,这会导致在 eclipse 控制台上显示红色字体:
我知道我可以通过编写自定义 Handler
来改变这种行为, 但我想知道为什么默认输出出现在 stderr
上而不是 stdout
?
记录器应该使用 stdout
对于 fine
+ info
并使用 stderr
对于 severe
水平。
最佳答案
java.util.logging API是根据 JSR 47: Logging API Specification 开发的.根据“Proposed Final Draft”中的更改日志,ConsoleHandler 始终使用 System.err。 JCP 页面还列出了 API 的原始作者,我认为只有这些名字才能真正知道您问题的答案。
也就是说,我认为起源来自 System.err API 文档。
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.
反对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.
记录映射到诊断而不是原始数据。将数据与诊断错误信息分开很重要,尤其是当管道处理在一起时,因为下游消费者只准备好接受数据信息而不是错误消息。参见 Confused about stdin, stdout and stderr?获取更多详细信息。
关于java - 为什么 java.util.logging.Logger 打印到 stderr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34156052/