How can I get log4j to print exceptions properly?

Using your code sample: private static final Logger _log = Logger. GetLogger(MyClass. Class); ... try{...} catch (Exception e) { //Change //_log.

Error("Error refreshing all prices", e); //To _log. Error("Error refreshing all prices", e. FillInStackTrace()); } You'll see all the stack trace displayed PS.

Make Logger a singleton...(check my declaration) just after declaring public class MyClass {.

Using your code sample: private static final Logger _log = Logger. GetLogger(MyClass. Class); ... try{...} catch (Exception e) { //Change //_log.

Error("Error refreshing all prices", e); //To _log. Error("Error refreshing all prices", e. FillInStackTrace()); } You'll see all the stack trace URL1. Make Logger a singleton...(check my declaration) just after declaring public class MyClass {.

Thanks for the advice! I will try this now. BTW, for the singleton approach you can't use 'this' with the static getLoggger().

I guess I can use getLogger(MyClass. Class) – Ryan Feb 19 '10 at 9:18 True Ryan!. It makes instantiation faster seeing that the singleton already exists after creation.

– The Elite Gentleman Feb 19 '10 at 9:22 1 that changes the stack trace to be the state of the current thread. It will no longer be the stack trace for the thrown exception – objects Feb 23 '10 at 22:43 1 the stack trace of the thread is different from the stack trace of the exception. The stack trace of the exception shows where the exception occurred, the stack trace of the thread (in this case) shows where the exception was caught.

– objects Feb 24 '10 at 8:45 1 fillInStackTrace does not record the cause afaik, it (the cause) is set when the exception is created (fillInStackTrace is also called at that point) – objects Feb 24 '10 at 23:55.

What you have posted should display the stack trace as stated in the javadoc. Note that if you don't include a message (and just call logger. Error(ex)) then the stack trace is not logged.

I don't see anything wrong with your config, so try to upgrade log4j to a newer (not necessarily the latest) version. Though not the problem in this case, you'd better make your loggers private static final.

I haven't used the fillStackTrace call, so I cannot comment if that will work. Another approach is to use a little method that returns the formatted text from an Exception. Public static String getStackTrace(Exception e) { StringWriter sWriter = new StringWriter(); PrintWriter pWriter = new PrintWriter(sWriter); e.

PrintStackTrace(pWriter); return sWriter.toString(); } In your logging code, you could write: logger. Error("An exception occurred: " + Utils. GetStackTrace(e)).

Ideally, he shouldn't need this – Bozho Feb 20 '10 at 10:48.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions