AOP实现日志拦截

版权声明: https://blog.csdn.net/niuch1029291561/article/details/52872020

使用Spring AOP实现日志拦截

类实现

package com.base.log;

import org.aopalliance.intercept.Joinpoint;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.jboss.logging.MDC;

//使用Throw通知类型来实现Advice
public class ExceptionLogHandler{
    private Logger logger = Logger.getLogger(this.getClass().getName());


    public void doThrowing(JoinPoint jp, Throwable ex) { 
    String className = jp.getTarget().getClass().getName();
    String methodName = jp.getSignature().getName();
        MDC.put("className", (Object)className);
        MDC.put("methodName", (Object)methodName);
        MDC.put("longMsg", (Object)limitStackTrace2String(ex));
        logger.log(Level.INFO, getMessage(ex));           
    }   
    
    private String getMessage(Throwable ex){
    StringBuffer sb = new StringBuffer();
    sb.append(ex.getMessage());
    if(ex.getCause() != null){
    sb.append(" Caused by:").append(ex.getCause().getMessage());
    }
    return sb.toString();    
    }
    
    private String limitStackTrace2String(Throwable ex){
        StringBuffer sb = new StringBuffer();
        sb.append(ex).append("\n");
        StackTraceElement[] st = ex.getStackTrace();
        for(int i=0;i<20;i++){
        sb.append(" at ").append(st[i]).append("\n");
        }
        sb.append(" ......").append("\n");
//        sb.append(" at ").append(st[0]).append("\n")
//        .append(" at ").append(st[1]).append("\n")
//        .append(" at ").append(st[2]).append("\n")
//        .append(" ......").append("\n");
        
        if(ex.getCause()!=null){
            sb.append("Caused by: ").append(ex.getCause()).append("\n");
            StackTraceElement[] st2 = ex.getCause().getStackTrace();
            for(int i=0;i<5;i++){
            sb.append(" at ").append(st2[i]).append("\n");
            } 
            sb.append(" ......").append("\n");
//            sb.append(" at ").append(st2[0]).append("\n")
//            .append(" at ").append(st2[1]).append("\n")
//            .append(" at ").append(st2[2]).append("\n")
//             .append(" ......").append("\n");
        }    
    return sb.toString();
    }

}

配置实现

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <!-- 启用acpect对注解的支持 -->
    <!-- <aop:aspectj-autoproxy></aop:aspectj-autoproxy> -->
    <bean id="exceptionLogHandler" class="com.base.log.ExceptionLogHandler" />
    <!-- proxy-target-class="true" -->
    <aop:config >
        <aop:aspect id="exceptionLogAspect" ref="exceptionLogHandler">
            <!-- <aop:pointcut id="addAddMethod" expression="execution(* add*(..))"/> -->


            <!-- 操作日志的处理 
            <aop:pointcut id="addAddMethod" expression="execution(* com.tgb.spring.*.add*(..)) ||execution(* com.tgb.spring.*.del*(..))"/>
            <aop:before method="checkSecurity" pointcut-ref="addAddMethod"/>
            <aop:after method="checkSecurity" pointcut-ref="addAddMethod"/>-->


            <!-- 异常日志的处理 -->  
            <!-- <aop:pointcut id="addExceptionMethod" expression="execution(* com.tgb.spring.*.*(..)) || args(name)"/> --> 
            <!-- 
            <aop:after-throwing method="doThrowing" pointcut="execution(* com.psi.inventory.service.*.*(..))" throwing="ex"/> 
             -->
            <aop:after-throwing method="doThrowing" pointcut="execution(* com..*Service.*(..))" throwing="ex"/>
        </aop:aspect>
    </aop:config>
</beans>


猜你喜欢

转载自blog.csdn.net/niuch1029291561/article/details/52872020