Java 为什么未登录异常堆栈跟踪?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21164917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is the exception stack trace not logged in?
提问by remi
I tried the simple example from SLF4J FAQ:
我尝试了SLF4J FAQ 中的简单示例:
package com.aed.tests.logging;
import org.slf4j.LoggerFactory;
public class TestLogging
{
public TestLogging()
{
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args)
{
String s = "Hello world";
try
{
Integer i = Integer.valueOf(s);
}
catch(NumberFormatException e)
{
LoggerFactory.getLogger("simplelogger").error("Failed to format {}", s, e);
LoggerFactory.getLogger("simplelogger").error("Without parametrized string", e);
}
}
}
Here is the output:
这是输出:
15:33:51,248000000 [SEVERE]simplelogger: Failed to format Hello world
15:33:51,275000000 [SEVERE]simplelogger: Without parametrized string
I use java.util.logging as the logging implementation, with the following logging.properties
我使用 java.util.logging 作为日志实现,具有以下 logging.properties
# Properties file which configures the operation of the JDK
# logging facility.
# The system will look for this config file, first using
# a System property specified at startup:
#
# >java -Djava.util.logging.config.file=myLoggingConfigFilePath
#
# If this property is not specified, then the config file is
# retrieved from its default location at:
#
# JDK_HOME/jre/lib/logging.properties
# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# (? LogManager docs say no comma here, but JDK example has comma.)
handlers=java.util.logging.ConsoleHandler
# Default global logging level.
# Loggers and Handlers may override this level
.level=ALL
# Loggers
# ------------------------------------------
# Loggers are usually attached to packages.
# Here, the level for each package is specified.
# The global level is used by default, so levels
# specified here simply act as an override.
# myapp.ui.level=ALL
# myapp.business.level=CONFIG
# myapp.data.level=SEVERE
# Handlers
# -----------------------------------------
# --- ConsoleHandler ---
# Override of global logging level
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
# HH:MM:ss,nanosec
java.util.logging.SimpleFormatter.format=%1$tT,%1$tN [%4$s]%3$s: %5$s %n
I expected to see the stacktrace of the exception. Am I missing something in the configuration? Shall I enable exception strack trace somehow, with SLF4J or jdk logging?
我希望看到异常的堆栈跟踪。我在配置中遗漏了什么吗?我应该以某种方式启用异常跟踪跟踪,使用 SLF4J 或 jdk 日志记录吗?
EDIT Following the "Marked as duplicate". My question was not "how to print the stack trace" but how comes, using the very example of SLF4J which itself does the job to print out the stack trace, I still had no stack trace printed out. As I said I had the stack trace printed out using log4j as implementation but not java.util.logging. So it was indeed a matter of wrong configuration of java.util.logging, and I gave an answer when I found out.
编辑在“标记为重复”之后。我的问题不是“如何打印堆栈跟踪”,而是如何使用 SLF4J 的示例本身来打印堆栈跟踪,我仍然没有打印出堆栈跟踪。正如我所说,我使用 log4j 作为实现而不是 java.util.logging 打印了堆栈跟踪。所以确实是java.util.logging配置错误的问题,发现后给出了答案。
采纳答案by remi
It was indeed a matter of properly configuring the formatter for log records.
这确实是为日志记录正确配置格式化程序的问题。
As the manual says, the throwable is the 6th parameter of the method format. So I added %6$s
to my format in logging.properties:
正如手册所说,throwable 是方法格式的第 6 个参数。所以我%6$s
在 logging.properties 中添加了我的格式:
java.util.logging.SimpleFormatter.format=%1$tT,%1$tN [%4$s]%3$s: %5$s %6$s %n
And here is the output, showing the stack trace:
这是输出,显示堆栈跟踪:
17:29:33,314000000 [SEVERE]simplelogger: Failed to format Hello world
java.lang.NumberFormatException: For input string: "Hello world"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.valueOf(Integer.java:582)
at com.aed.tests.logging.TestLogging.main(TestLogging.java:15)
17:29:33,344000000 [SEVERE]simplelogger: Without parametrized string
java.lang.NumberFormatException: For input string: "Hello world"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.valueOf(Integer.java:582)
at com.aed.tests.logging.TestLogging.main(TestLogging.java:15)
回答by Limited Atonement
java.lang.Exception.toString()
doesn't return the stack trace information. From the documentation:
java.lang.Exception.toString()
不返回堆栈跟踪信息。从文档:
Returns a short description of this throwable
返回此 throwable 的简短描述
see How can I convert a stack trace to a string?, and log the String
you get back.
请参阅如何将堆栈跟踪转换为字符串?,并记录String
你回来。