【发布时间】:2014-11-02 08:01:25
【问题描述】:
我正在使用 Spring AOP 来减少我现有应用程序的调试日志。
我尝试做的是根据日志级别记录每个方法调用。
如果我使用以下方面,我知道 Spring 将为我的每个类构建一个代理,如果它不在调试级别,则会引入一些开销:
package com.basepackage.aop;
import....
@Aspect
@Component
public class LogAspect {
private Logger logger=Logger.getLogger(LogAspect.class.getName());
@Pointcut("execution(* com.basepackage..*.*(..))")//all the method in my app
private void debug_log(){};
@Around("debug_log()")//here, I hope I can introduce something like && logger.isDebugeEnable()
public Object aroundLog(ProceedingJoinPoint joinPoint) throws Throwable{
String signature=joinPoint.getSignature().toString();
String paramList = null;
Object[] args=joinPoint.getArgs();
for (int i = 0; i < args.length; i++) {
paramList+=args[i]+" ";
}
String debugMsg="----------enter "+signature;
if(paramList!=null){
debugMsg+="\nparam: "+paramList;
}
LogUtil.debug(debugMsg);//will delegate to log4j
try{
Object returnObject= joinPoint.proceed();
LogUtil.debug("--------return form"+signature);//will delegate to log4j
return returnObject;
}
catch(Throwable t){
LogUtil.error("--------error from "+signature, t);//will delegate to log4j
throw t;
}
}
}
我希望只有当 log4j 级别
或任何关于如何使日志干净的建议将不胜感激。
谢谢!
【问题讨论】:
标签: java spring log4j aspectj spring-aop