java Log4j 不打印堆栈跟踪
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5662582/
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
Log4j does not print stack trace
提问by Erdin? Ta?k?n
I catch NullPointerException but log4j does not print stack trace, I aspect number of line of exception occurred etc. what is wrong?
我捕获了 NullPointerException 但 log4j 不打印堆栈跟踪,我出现异常的行数等。有什么问题?
My log
我的日志
20110412-101042,317[ Timer-7][R] Exception while processing for value: abc. [xyz.Dummy]
java.lang.NullPointerException
log4j.property
file
log4j.property
文件
log4j.rootCategory=ERROR, logfile
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p %r [%t] : %m%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=my_application.log
log4j.appender.logfile.Append=true
log4j.appender.logfile.MaxBackupIndex =10
log4j.appender.logFile.MaxFileSize=40000KB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyyMMdd-HHmmss,SSS}[%8.8t][%.1p] %-70m[%c{2}]%n
My snippet code
我的代码片段
String value;
try {
value = "abc";
//... lots for code
}catch(Exception e) {
logger.error("Exception while processing for value: " + value + ". ", e);
}
采纳答案by Daniel
The problem is the %-70m
in your Layout. It truncates the message, and therefore does not reach the stacktrace. Use %m
as usual instead.
问题出%-70m
在您的布局中。它会截断消息,因此不会到达堆栈跟踪。%m
照常使用。
Even Better: Write a custom layout, this will work as you want.
更好:编写自定义布局,这将如您所愿。
回答by ACrescendo
Your code only shows the exception message.If you want to see stack trace ,you must use something like that:
您的代码仅显示异常消息。如果您想查看堆栈跟踪,则必须使用类似的内容:
How to store printStackTrace into a string
Try to use that one instead of exception 'e'.
尝试使用那个而不是异常“e”。
回答by Selva
Anybody who wants to print the line number to know where null pointer exception occurred without printing the full stacktrace, please try like below:
任何想打印行号以了解空指针异常发生的位置而不打印完整堆栈跟踪的人,请尝试如下:
try {
// your code here
}catch(NullPointerException ne){
System.out.println("NullPointerException : LineNumber:"+ne.getStackTrace()[0].getLineNumber()+" : "+ne);
}