Log4j does not print stack trace

I am catching a NullPointerException, but log4j does not print the stack trace, I am the aspect line number of the exception, etc. what is wrong?

My magazine

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); } 
+6
source share
3 answers

The problem is %-70m in your layout. It truncates the message and therefore does not reach the stacktrace. Use %m instead.

Even better: write your own layout, it will work the way you want.

+4
source

An exception message is displayed in your code. If you want to see the stack trace, you should use something like this:

How to save printStackTrace to string

Try using this instead of the 'e' exception.

-1
source

Anyone who wants to print the line number to find out where the null pointer exception occurred without printing the full stacktrace, try as shown below:

 try { // your code here }catch(NullPointerException ne){ System.out.println("NullPointerException : LineNumber:"+ne.getStackTrace()[0].getLineNumber()+" : "+ne); } 
-1
source

Source: https://habr.com/ru/post/885901/


All Articles