溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

切入Debug日志

發(fā)布時(shí)間:2020-07-17 12:54:41 來源:網(wǎng)絡(luò) 閱讀:287 作者:isiah_zhou 欄目:開發(fā)技術(shù)

注意:Action如果實(shí)現(xiàn)了Action接口,或者繼承了ActionSupport類,則要在aop:config標(biāo)簽添加 proxy-target-class="true",否者會(huì)拋異常。

新建文件: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問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI