I catch NullPointerException but log4j does not print stack trace, I aspect number of line of exception occurred etc. what is wrong?
My log
20110412-101042,317[ Timer-7][R] Exception while processing for value: abc. [xyz.Dummy]
java.lang.NullPointerException
log4j.property
file
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);
}
The problem is the %-70m
in your Layout. It truncates the message, and therefore does not reach the stacktrace. Use %m
as usual instead.
Even Better: Write a custom layout, this will work as you want.
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'.
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.