我使用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
答案 0 :(得分:6)
我认为这不是Spring AOP问题,而只是Log4j的工作方式,请参阅PatternLayout的Javadoc:
<强>→强>
用于输出发出日志记录请求的行号 。
警告生成调用者位置信息非常慢,应该避免,除非执行速度不是问题。
所以我的建议是使用没有行号的模式布局,并使用Spring AOP确定行号的功能,大致如下:
joinPoint.getSourceLocation().getLine()