温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

切入Debug日志

发布时间:2020-07-17 12:54:41 来源:网络 阅读:276 作者:isiah_zhou 栏目:开发技术

注意: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>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI