views:

73

answers:

4

I was catching an exception and trying to write the stack trace to the logs like this:

log.warn(e.getMessage());

But all it said was

null

So I changed it to

log.warn(e.toString());

And now it says only

java.lang.NullPointerException

How do I write the full stack trace to the log so I can see where this Exception is being generated in the app?

+5  A: 

Usually:

log.warn("message", e);

But it depends on your logging framework too.

Peter Štibraný
+2  A: 

You can use

logger.log(Level.WARN, "logged exception", ex);

or

logger.warn("logged exception", ex);

Resources :

Colin Hebert
+1  A: 
Noel M
if e was null, log.warn(e.getMessage()); would throw exception and it would not print "null".
Peter Štibraný
No it wouldn't, it would throw a NPE
Noel M
Sorry, I misread your comment
Noel M
A: 

Maybe you are looking for something like this: http://www.javapractices.com/topic/TopicAction.do?Id=78

Joelio