Skip to main content

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

Hi,

In order to increase performance I'm trying to solve the following
problem using aspectj:

When logging, we would like to do something like this:

logger.debug("debug: " + test);

the entry is written to the log only if the DEBUG log level is enabled.
_However_, the arguments passed to Logger.debug are evaluated, even if
only the ERROR log level is enabled. If the entry should not be logged,
evaluating the arguments is unneccessary and can cause a lot of overhead
(in the above case the toString method of test is executed and a new String
object is created....) 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);
}

although this works, the code is getting quite messy.

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 (?) since the arguments are
evaluated before reaching the around advise. :( I even tried to remove
the Object o and the args(o) bindings, but that didn't help either.

Does anyone have an idea how to solve this problem?

Thanks,
joda





Back to the top