• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Exception stack trace in logger

 
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
 
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
 
Sheriff
Posts: 10445
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?
 
Author
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
Boost this thread!