Reputation: 1578
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);
}
Upvotes: 5
Views: 13483
Reputation: 338
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);
}
Upvotes: -2
Reputation: 56
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'.
Upvotes: -1
Reputation: 28074
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.
Upvotes: 4