Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print stack trace in log file

Tags:

java

log4j

I am loging error im my java swing application: logger.error("ERROR: " + ex); where ex is exception

this just print me 13:33:58,964 ERROR PlayOffPanel:292 - ERROR: java.lang.NullPointerException

but I wanna know stacktrace.

my log4j properites:

log4j.rootLogger=DEBUG,file

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=error.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
like image 791
hudi Avatar asked Apr 06 '12 08:04

hudi


People also ask

How do I print stack trace logging?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.

Which of the method is used to print stack trace?

printStackTrace() method prints this throwable and its backtrace to the standard error stream. It prints a stack trace for this Throwable object on the error output stream that is the value of the field System.

What is stack trace log?

A stack trace shows the call stack (sets of active stack frames) and provides information on the methods that your code called. Usually, a stack trace is shown when an Exception is not handled correctly in code. (An exception is what a runtime environment uses to tell you that there's an error in your code.)


Video Answer


1 Answers

You have to write

logger.error("Error description",ex);

Look at the javadoc, the exception must be the second arguments if you want stacktrace.

like image 85
dash1e Avatar answered Sep 21 '22 22:09

dash1e