Spring Aop日志记录行号不正确

Spring Aop日志记录行号不正确,spring,logging,aop,Spring,Logging,Aop,我正在使用spring aop为我的应用程序进行日志记录: 我配置了前后提示,但我看到的行号不是目标类的行号,而是用于日志记录的类的行号 我怎样才能解决这个问题 下面是我的配置 Spring xml: <aop:aspectj-autoproxy proxy-target-class="false" /> 用于日志记录的类: package com.digilegal.services.ahc.logging; import java.lang.reflect.Modifier;

我正在使用spring aop为我的应用程序进行日志记录: 我配置了前后提示,但我看到的行号不是目标类的行号,而是用于日志记录的类的行号 我怎样才能解决这个问题 下面是我的配置

Spring xml:

<aop:aspectj-autoproxy proxy-target-class="false" />
用于日志记录的类:

package com.digilegal.services.ahc.logging;

import java.lang.reflect.Modifier;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

@Aspect
public class AHCLogging {

    @Before("execution(* com.digilegal.services..*.*(..))")
    public void logBefore(JoinPoint joinPoint) {

        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.trace("ENTER METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
        }

    }

    @After("execution(* com.digilegal.services..*.*(..))")
    public void logAfter(JoinPoint joinPoint) {
        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature)     joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.trace("EXIT METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
        }
    }

    @AfterThrowing(pointcut = "execution(* com.digilegal.services..*.*        (..))",throwing= "error")
    public void logAfterThrowing(JoinPoint joinPoint, Throwable error) {
        Logger log = Logger.getLogger(joinPoint.getTarget().getClass());
        MethodSignature signature = (MethodSignature)     joinPoint.getSignature();

        if (!Modifier.isPrivate(signature.getModifiers())
                && !signature.getName().startsWith("get")
                && !signature.getName().startsWith("set")
                && !signature.getName().startsWith("is")) {
            log.error("EXCEPTION IN METHOD ::"
                    + signature.getReturnType().getSimpleName() + " "
                    + signature.getName() + "("
                    + paramterType(signature.getParameterTypes()) + ")");
            log.error("Exception",error);
        }
    }

    private String paramterType(Class<?>[] classes) {
        StringBuffer buffer = new StringBuffer();
        String returnValue = "";

        for (Class<?> string : classes) {
            buffer.append(Modifier.toString(string.getModifiers()));
            buffer.append(" ");
            buffer.append(string.getSimpleName());
            buffer.append(",");
        }

        returnValue = buffer.toString();

        if (returnValue.trim().length() > 0) {
            returnValue = returnValue.substring(0, returnValue.length() -         1);
        }

        return returnValue;
    }
}
我是错过了什么,还是应该是这样

谢谢


Nirav

我认为这不是Spring AOP的问题,而是Log4j的工作方式,请参阅Javadoc了解:

L

用于输出发出日志记录请求的行号

警告生成呼叫者位置信息的速度非常慢,除非执行速度不是问题,否则应该避免

因此,我建议使用不带行号的模式布局,并使用Spring AOP确定行号的功能,大致如下所示:

joinPoint.getSourceLocation.getLine
我认为这不是一个Spring AOP问题,而是Log4j的工作方式,请参阅Javadoc了解:

L

用于输出发出日志记录请求的行号

警告生成呼叫者位置信息的速度非常慢,除非执行速度不是问题,否则应该避免

因此,我建议使用不带行号的模式布局,并使用Spring AOP确定行号的功能,大致如下所示:

joinPoint.getSourceLocation.getLine
里面没有提到行号,所以我不知道你在问什么。您还知道,spring已经有了一个类,可以为您记录这些内容,而不是添加您自己的?。查一下酷谢谢你指出春季课程会有很大帮助但我要说的是:4月4日,2015 15:25:08:775 TRACE LoginBean:30-输入方法::void doLoginpublic ActionEvent,其中30是日志类的行号,而不是LoginBear,其中没有提到行号,因此我不确定您在问什么。您还知道,spring已经有了一个类,可以为您记录这些内容,而不是添加您自己的?。查看“酷”感谢您指出Spring类这会有很大帮助,但我要说的是:2015年4月4日15:25:08:775跟踪LoginBean:30-输入方法::void doLoginpublic ActionEvent,其中30是日志类的行号,而不是LoginBanhi Kriegaex,感谢这一帮助Sweel,如果是这样的话,请接受并更新我的答案。Spring AOP 4.2.2之前不支持此方法joinPoint.getSourceLocation.getLine。线程主java.lang.UnsupportedOperationExceptionI中的异常我不是Spring用户,但您看到了吗?我不知道这是否有帮助。不管怎样,这个问题中的问题是Log4J的用法错误。不好意思,Spring4.2.4也不起作用。。。该死!您好,Kriegaex,谢谢您的帮助。如果有,请接受并更新我的答案。Spring AOP直到4.2.2都不支持此方法joinPoint.getSourceLocation.getLine。线程主java.lang.UnsupportedOperationExceptionI中的异常我不是Spring用户,但您看到了吗?我不知道这是否有帮助。不管怎样,这个问题中的问题是Log4J的用法错误。不好意思,Spring4.2.4也不起作用。。。该死!