dcsimg
How to log exception stack traces to log file but not the console at any log4j level?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Alwyn_Teh
Posted On:   Wednesday, January 25, 2006 12:44 PM

I've got a properties file set up at DEBUG level for the rootLogger, then using Thresholds for the console and a file appender to choose what to log. The console is used by the end user and I would like just simple 1 line error messages to be displayed. I also want to log exception stack traces to a log file, so that I will never miss them, no matter what log4j level is used for the file appender. It seems log4j only allow me to use Thresholds, but then I'd have to set the exceptions to print stack traces at DEBUG level, whereas exceptions are really of type ERROR. But if I set them to ERROR, then they will appear on the console as well. A lot of the times   More>>

I've got a properties file set up at DEBUG level for
the rootLogger, then using Thresholds for the console
and a file appender to choose what to log.

The console is used by the end user and I would like
just simple 1 line error messages to be displayed.

I also want to log exception stack traces to a log file,
so that I will never miss them, no matter what log4j level
is used for the file appender.

It seems log4j only allow me to use Thresholds, but then
I'd have to set the exceptions to print stack traces at
DEBUG level, whereas exceptions are really of type ERROR.
But if I set them to ERROR, then they will appear on the
console as well. A lot of the times, I don't want to leave
the application running in DEBUG as I will then get huge
logfiles.

Now, I'm trying to find a workaround...

I have tried to get the FileAppender from the rootLogger
using getAllAppenders. When I iterate over the Enumeration
returned, I can't cast it to DailyRollingFileAppender
because it would throw a runtime ClassCastException.
So, I only got the string org.apache.log4j.DailyRollingFileAppender@af993e.

Later on, I call rootLogger.getAppender( "org.apache.log4j.DailyRollingFileAppender" ).doAppend( le ) where le is a LoggingEvent.
Now, I get NullPointerException!

Perhaps, this isn't going to work either as the LoggingEvent has a log4j level too. Well, how about just writing to the file directly bypassing the Appender?

Anybody any ideas on how to get log4j to do what I want?

   <<Less

Re: How to log exception stack traces to log file but not the console at any log4j level?

Posted By:   ankush_purwar  
Posted On:   Wednesday, January 25, 2006 05:17 PM

I think you are doing e.printStackTrace()
this will cause to print exception on console.
you should capture this exception into some string and log the string into file.
try to use e.printStackTrace(printWriter)
and try to make object of printWriter by using StringWriter.

Hope, it will help you.
About | Sitemap | Contact