[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [aspectj-users] Fixing logger.debug(expensiveObject.toString())
- From: "Wes Isberg" <wes@xxxxxxxxxxxxxx>
- Date: Tue, 6 Dec 2005 11:26:54 -0800
- Delivered-to: email@example.com
Hi Scott -
Your analysis is correct. At the time of the logger.debug(..) method-call
join point, the expensive toString() expression has already run. There is
no good lookahead.
More and more I bite the bullet and use an API that defers rendering
to the log method, e.g.,
printf("foo %s", expensiveObject)
or more generally
debug(String format, Object... stuff)
In that case, the expensive operation can be avoided using around advice.
That refactoring is good where it helps you consolidate rendering operations
rather that re-engineering toString() for different clients, but it's bad
if you write a god-renderer that knows too much about target classes.
> ------------Original Message------------
> From: Scott Hayward <shayward@xxxxxxxxxx>
> To: aspectj-users@xxxxxxxxxxx
> Date: Tue, Dec-6-2005 10:12 AM
> Subject: [aspectj-users] Fixing logger.debug(expensiveObject.toString())
> I'm looking at a piece of code where there are calls that look like
> where the toString() operation is expensive. Unfortunately, the
> call gets evaluated in the production system even though it is never
> written, and the application takes a performance hit. The problem is
> the code does not check whether debug logging is on. It should really
> coded like this:
> if logger.isDebug()
> ----- or -------
> // if the debug method checks the level before doing the
> you could do this
> 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
> 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
> 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
> only works if the sole reason to evaluate the expensive toString() is
> 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();
> Have I missed another approach?
> 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 /
> email: shayward@xxxxxxxxxx Web: http://www.can.ibm.com/pdc
> aspectj-users mailing list