+-

我使用 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 for PatternLayout:
L
Used to output the line number from where the logging request was issued.
WARNING Generating caller location information is extremely slow and should be avoided unless execution speed is not an issue.
所以我的建议是使用没有行号的模式布局,并使用Spring AOP确定行号的功能,大致如下:
joinPoint.getSourceLocation().getLine()
点击查看更多相关文章
转载注明原文:Spring Aop日志记录行号不正确 - 乐贴网