Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[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



Back to the top