RE: conditional logging of exception

2008-03-21 Thread pico
Thank you for your response.

(A) Sometimes we run third party libraries in a container that are very noisy.  
Given this situation, we have two choices:
(1) log the exception and fill up the log file,
(2) decrease the logging level and miss the exception entirely.

It seems like it would be nice to have a third state: log a one liner summary 
of the error.

(B) Sometimes exceptions are not exceptional.  Their generation depends on the 
mood, at the time, of whatever developer coded the library you are using.

-

The class org.apache.log4j.Logger seems like it is coded with these 
distinctions in mind, since there are two APIs for any given log level:

logger.debug(Object)
logger.debug(Object, Throwable)
logger.error(Object)
logger.error(Object, Throwable)

and so on.

Part of the rationale for log4j is that you can adjust the log level (and thus 
the log output) without changing code.  In the end, I suppose what I am driving 
at is, for each level, there should be a third API, that controls whether 
logger.error(O) or logger.error(O, T) is used, based on the current logger 
Level.

Level.FATAL: log nothing
Level.ERROR: log the Object
...
Level.DEBUG: log the Object and the stack trace

Since this API doesn't exist, I'm trying to work around it.

-

With regard to the suggestion of using the FQCN API, there seems to be a 
problem using it with the Appender layout options %C:%-4L.  When I use 
BasicConfigurator.configure(), I get:

.0 [main] ERROR mycompany.test.LoggerTest  - java.lang.Exception
0 [main] ERROR mycompany.test.LoggerTest  - java.lang.Exception
java.lang.Exception
at mycompany.test.LoggerTest.logSomeStuff(LoggerTest.java:36)
at mycompany.test.LoggerTest.testBasicLogging(LoggerTest.java:17)

which is what I want.  If I configure my logger with the appender layout:

%d [%t] %-5p %C:%-4L - %m%n

... then I get:

2008-03-21 11:00:42,338 [main] ERROR ?:?- java.lang.Exception
2008-03-21 11:00:42,354 [main] ERROR ?:?- java.lang.Exception
java.lang.Exception
at mycompany.test.LoggerTest.logSomeStuff(LoggerTest.java:36)
at mycompany.test.LoggerTest.testSiteLogging(LoggerTest.java:27)

So FQCN affects the first line of the stack trace, which is good, but it messes 
up the class and line number in the pattern, which is bad.




-Original Message-
From: Bender Heri [EMAIL PROTECTED]
Sent: Mar 20, 2008 5:27 PM
To: Log4J Users List log4j-user@logging.apache.org, [EMAIL PROTECTED]
Subject: RE: conditional logging of exception




-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



RE: conditional logging of exception

2008-03-20 Thread Bender Heri
I dont really understand your thoughts why an error should not be logged along 
with stack trace, since it is an exceptional situation which should not occur 
often. Errors in logs are never noise ratio.

But your problem with classname, linenummer etc. can be solved if you use the 
overloaded log method which takes as first parameter the FQCN. FQCN is then the 
one of the calling class.

Your static method:
  public static void logVerboseOnLevel( Object instance, Logger logger, Level 
thresholdLevel, Exception e, Level logAtLevel) 
  {
 if (logger.isEnabledFor(thresholdLevel)) 
 {
 logger.log( instance.getClass.getName(), logAtLevel, e, e);
 } else {
 logger.log( instance.getClass.getName(), logAtLevel, e);
 }
 }
 
 and called like:
 
 Logger logger = Logger.getLogger(getClass);
 Wrapper.logVerboseOnLevel( this, logger, Level.DEBUG, new 
 Exception(), Level.ERROR);

 NOT TESTED, just hacked in by mind.

Heri




 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
 Sent: Thursday, March 20, 2008 8:07 PM
 To: log4j-user@logging.apache.org
 Subject: [SPAM (Bayesain Analysis)] - conditional logging of 
 exception -
 Bayesian Filter detected spam
 
 
 Hello,
 
 For a webapp, it would be nice to be able to conditionally 
 log stack trace along
 with exception.
 
 Scenario:
 
 (1) Production application ships with Level set to Level.ERROR.
 (2) An app admin notices some errors in the log 
 [Logger.error(Object)].
 (3) A means is available [Logger.setLevel()] to dynamically 
 set Level to Level.DEBUG
 for a particular class.
 (4) When the error occurs again, the stack trace is logged 
 along with the error 
 [Logger.error(Object, Throwable)].
 
 A solution would be to have a wrapper class with a static method like:
 
 public static void logVerboseOnLevel(Logger logger, Level 
 thresholdLevel, Exception
 e, Level logAtLevel) {
 if (logger.isEnabledFor(thresholdLevel)) {
 logger.log(logAtLevel, e, e);
 } else {
 logger.log(logAtLevel, e);
 }
 }
 
 and called like:
 
 Logger logger = Logger.getLogger(getClass);
 Wrapper.logVerboseOnLevel(logger, Level.DEBUG, new 
 Exception(), Level.ERROR);
 
 If logger level is ERROR, just the one liner is logged.  If 
 logger level is DEBUG,
 the stack trace is logged.
 
 The benefit is to minimize the amount of information in the 
 log file, unless the
 cause of the exception is being sought.  Since it is possible 
 to set log level per
 class, the signal to noise ratio is much more conducive to 
 finding the problem.
 
 In log4j.properties, the appender is defined like:
 
 log4j.appender.MY_APPENDER.layout.ConversionPattern=%d [%t] 
 %-5p %c:%l %C:%-4L
 - %m%n
 
 The portion %C:%-4L means I want the class and line number where the
 problem occurred.  This conflicts with the wrapper, as its 
 class and line number
 are the ones used to invoke log4j.  Also, it would be nice to 
 only use the wrapper
 class in specialized situations.  So any solution would need 
 to handle both the 
 straightforward Logger use case, as well as the wrapper.
 
 There is an old post on this forum describing a similar 
 problem, but I couldn't
 really figure out how to utilize the responses.
 
 http://mail-archives.apache.org/mod_mbox/logging-log4j-user/20
0710.mbox/[EMAIL PROTECTED]

Originally I had the chunk of code above copied in each specialized place where 
I needed it, then I refactored the copies into a utility class, and ran into the
logging output issue.

Any suggestions for a solution to this usage case?  

Thanks...





-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]