Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception stack trace in logger

 
M Mehta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to log through code like log.info, log.error, etc in your code....
 
M Mehta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
logger.error("Error in the handler" + e);


Have you tried the overloaded method which accepts a Throwable for the second param? Like this one.
 
M Mehta
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic