Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] preventing argument evaluation before an around advise

On Thu, 24 Jun 2004 14:28:01 +0200, joda <aspectj-users-joda@xxxxxxxxxxxx> wrote:

In order to increase performance I'm trying to solve the following
problem using aspectj:
...
logger.debug("debug: " + test);
...
_However_, the arguments passed to Logger.debug are evaluated, even if
only the ERROR log level is enabled.
...
I would like to get rid of this overhead.

One solution is to do the following:

if (Level.DEBUG.isGreaterOrEqual(logger.getLevel())) {
    logger.debug("debug: " + test);
}

That is the recommended way to write log statements. Maybe (depending on the logger)
it can be made slightly shorter by writing:
---
  if (logger.isDebugEnabled()) {
    logger.debug("debug: " + test);
  }
---
I know there is a plugin for Eclipse that can insert log statements for you.

although this works, the code is getting quite messy.

True.

So, I've tried to solve it using aspectj and the following advise:

void around(Logger logger, Object o): target(logger) &&
                                      args(o) &&
                                      call(void debug(Object)) {
    if (Level.DEBUG.isGreaterOrEqual(logger.getLevel())) {
        proceed(logger, o);
    }
}

Unfortunately, this don't work as expected (?)

Indeed, and it is the expectation that is to blame.
This pointcut is around the actual call joinpoint. It is not around the
method call statement nor the line it is on. That is, it happens after
the arguments have been evaluated.

since the arguments are evaluated before reaching the around advise. :(

Exactly. That's what a call joinpoint is.

Does anyone have an idea how to solve this problem?

Not using an aspect, no. There is no joinpoint for the evaluation of an expression, or generally for anything happening purely inside a method body (except exception handler joinpoints, but it's not good style to throw and catch an exception inside
the same method :).

Hmm. Maybe, if the logger is declared as a field, you can trigger on field references to it. But that would not distinguish log.debug from log.fatal. No, I don't think it's
possible to solve this with aspects.


/L
--
Lasse R. Nielsen - atwork@xxxxxxxxxx
 'Faith without judgement merely degrades the spirit divine'


Back to the top