AOP實現日誌攔截
使用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 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){
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(" ......").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>