切入Debug日志

AOP与日志记录
注意:Action如果实现了Action接口,或者继承了ActionSupport类,则要在aop:config标签添加 proxy-target-class="true",否者会抛异常。
新建文件:DebugLogger.java:

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;

public class DebugLogger{

Logger logger = Logger.getLogger(DebugLogger.class);

StringBuffer buffer = new StringBuffer();

public void log(JoinPoint joinPoint) {

Object[] args = joinPoint.getArgs();
buffer.delete(0, buffer.length()).append("enter function: ").append(joinPoint.getSignature()).append(", parameters: ");
if (args != null) {
for (Object obj : args) {
buffer.append(obj);
}
}
if (logger.isDebugEnabled())
{
logger.debug(buffer.toString());
}
}

public void logWithReturn(JoinPoint joinPoint, Object returnObj) {
buffer.delete(0, buffer.length()).append("exit function:").append(joinPoint.getSignature()).append(", return value:").append(returnObj);
if (logger.isDebugEnabled())
{
logger.debug(buffer.toString());
}
}
}
-----------------------------------------------------------默默无闻的分割线-----------------------------------------------------------
更改applicationContext.xml文件:
<bean id="debugLogger" class="com.shenzhen.management.util.log.DebugLogger"></bean>

<aop:config proxy-target-class="true">
<aop:pointcut id="logPointcut" expression="execution(* com.shenzhen.management.action.*.*(..))
or execution(* com.shenzhen.management.service.*.*(..))
or execution(* com.shenzhen.management.dao.*.*(..))"/>
<aop:aspect id="logAspect" ref="debugLogger">
<aop:before method="log" pointcut-ref="logPointcut"/>
<aop:after-returning method="logWithReturn" returning="returnObj" pointcut-ref="logPointcut"/>
</aop:aspect>
</aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值