aspose file tools*
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
Author

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 l4j.properties I have created:



Thanks
Gokul Nanthakumar
Ranch Hand

Joined: May 11, 2010
Posts: 56
You need to log through code like log.info, 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.

Thanks,
Me
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10216
    
166

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
Author
Rancher

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.
 
wood burning stoves
 
subject: Exception stack trace in logger