开发者

How to original log line numbers with AspectJ and log4j?

How can I log original line numbers of my method with AspectJ(Spring) framework? I'm new to aop programming, so I just want to know if it's possible or how to? Since aop will delegate the invocation process of my method, which generates new class and new method, line numbers logged are always not original ones.

Below are some of my codes:

Schema-based aop configuration:

<bean id="logInterceptor" class="com.fuhu.appsub.aop.LogInterceptor"></bean>
<aop:config>
    <aop:aspect id="logDBAspect" ref="logInterceptor">
    <aop:pointcut id="logDBPointcut" expression="execution(*            com.fuhu.appsub.service..*(..)) " />
        <aop:after-throwing  pointcut-ref="logDBPointcut" throwing="ex" method="logDBException"/>           
    </aop:aspect>
</aop:config>

Here's my delegated method:

@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
public List<Item> findByName (String name) throws Exception{
    try
    {
        List<Item> itemList = itemRepository.findByName(name);
        int i=0,j=1;
        int k = j/i;
        return itemList;
    }
    catch(Exception ex)
    {
        throw new Exception(ex.getMessage() + "in findByName with name=" +开发者_StackOverflow社区 name + "  file:" + Thread.currentThread().getStackTrace()[2].getFileName() + "  line:" + Thread.currentThread().getStackTrace()[2].getLineNumber());
    }
}

Here's my delegating method:

 public void logDBException( JoinPoint joinPoint, Exception ex) {
         if(loggerDB.isErrorEnabled()){
             loggerDB.error(ex.getMessage());
         }
  }


Are you using full AspectJ or just Spring's AOP? Spring AOP is primarily based on Java dynamic proxies. It's usually enough to do what you need, it's fairly straightforward, and it doesn't really interfere with stack trace line numbers at all.


Even real AspectJ does not change the line number of your original code.

That is because the line number in class files is stored in the line number table. And this table allows it to have multiple statements in one "line". So even the extra AOP invocations must not change the line number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜