File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Other Open Source Projects and the fly likes Exception stack trace in logger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Exception stack trace in logger" Watch "Exception stack trace in logger" New topic

Exception stack trace in logger

M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
Hi All,

I am using log4j for logging. I want that whenever any exception comes in the application it should get printed in the log file. I am not able to find way to do this. Can you please help me? here is the I have created:

Gokul Nanthakumar
Ranch Hand

Joined: May 11, 2010
Posts: 56
You need to log through code like, log.error, etc in your code....
M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
I am using the logging functions already to log the errors.

but it doesn't print the complete exception trace to the log file, I have tried with e.getLocalizedMessage() and e.toString() methods as well.

I want the exception to get logged in the logger the way it is printed on the console, with complete stack trace. I am sure it should be possible.

Jaikiran Pai

Joined: Jul 20, 2005
Posts: 10441

logger.error("Error in the handler" + e);

Have you tried the overloaded method which accepts a Throwable for the second param? Like this one.

[My Blog] [JavaRanch Journal]
M Mehta
Ranch Hand

Joined: Aug 03, 2006
Posts: 92
Thanks for the response Jaikiran, the method with Throwable as argument is working fine

One more problem which I have to resolve. In the code, many times exception may come which has not been handled. I want in case of any exception the track trace should get printed to the log file, the way its done on the console. Is this possible with Log4j?
David Newton

Joined: Sep 29, 2008
Posts: 12617

Log4J doesn't magically wrap uncaught or unlogged exceptions. I seem to remember an uncaught exception listener or something you could attach a log statement to, though.
I agree. Here's the link:
subject: Exception stack trace in logger
It's not a secret anymore!