Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [aspectj-users] Fixing logger.debug(expensiveObject.toString())

Scott,
 
You should be able to trap all calls to logger.debug statement something like the following:
 
 public pointcut insideToString(org.apache.log4j.Logger logger) :
            call(* logger.debug(..)) && target(logger);
      void around(org.apache.log4j.Logger logger) : insideToString(logger)
      {
            if (logger.isDebug())
            {
                  return proceed();
            }
      }

Please note this is an eample and UN-TSTED code.
 
Ron

________________________________

From: aspectj-users-bounces@xxxxxxxxxxx on behalf of Scott Hayward
Sent: Tue 12/6/2005 1:12 PM
To: aspectj-users@xxxxxxxxxxx
Subject: [aspectj-users] Fixing logger.debug(expensiveObject.toString())



Hi,

I'm looking at a piece of code where there are calls that look like this:

logger.debug(expensiveObject.toString());

where the toString() operation is expensive. Unfortunately, the toString()
call gets evaluated in the production system even though it is never
written, and the application takes a performance hit. The problem is that
the code does not check whether debug logging is on. It should really be
coded like this:

if logger.isDebug()
{
     logger.debug(expensiveObject.toString());
}

----- or -------

// if the debug method checks the level before doing the object.toString(),
you could do this
logger.debug(expensiveObject);


But, as it happens, that's not what's in the code base.

My problem is that the toString() call is evaluated before the call to the
debug method is made. So I can't find a pointcut that will insert advice to
do a logger.isDebug() test---by the time I know I'm in a debug method (and
not, say, logger.error), it's too late.

I could put wrap the toString method of expensive objects with around
advice and replace the expensive operation if I'm not debugging. But this
only works if the sole reason to evaluate the expensive toString() is for
debug logging and *nothing* else. So if the application uses the same
toString() method elsewhere, the solution cannot be used.

      public pointcut insideToString() :
            call(* sandbox.BigUglyLogger.toString());

      String around() : insideToString()
      {
            if (BigUglyLogger.isDebug())
            {
                  return proceed();
            }
            return thisJoinPointStaticPart.getSignature().toShortString();
      }

Have I missed another approach?


Thanks,

Scott.
_______________________________________________________
Scott Hayward, Senior IT Specialist
IBM Canada Ltd., Pacific Development Centre
4611 Canada Way,  Burnaby, BC, V5G 4X3 CANADA
Tel (778) 327-7654          Fax (778) 327-3030      4 / PA1 / 4611 / BURN
email:  shayward@xxxxxxxxxx    Web: http://www.can.ibm.com/pdc

_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users




Back to the top