笔记:
如果controller层不起作用,可以在application.xml中加:
<aop:aspectj-autoproxy proxy-target-class="true"/>
如果还没有效果,加如下配置:
在:springmvc-servlet.xml中,引入application.xml
<import resource="classpath:applicationContext.xml"/>
application.xml的配置
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 系统服务组件的切面Bean -->
<bean id="aspectService" class="com.alphay.utils.AspectService"></bean>
<!-- AOP配置 -->
<aop:config>
<!-- 声明一个切面,并注入切面Bean,相当于@Aspect -->
<aop:aspect id="simpleAspect" ref="aspectService">
<!-- 配置一个切入点,相当于@Pointcut -->
<aop:pointcut expression="execution(* com.alphay.data.controller..*.*(..))" id="simplePointcut"/>
<aop:before pointcut-ref="simplePointcut" method="before"/>
<aop:after-throwing pointcut-ref="simplePointcut" method="afterThrow" throwing="ex"/>
</aop:aspect>
</aop:config>
具体拦截的类:
package com.alphay.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
/**
* Copyright: Copyright (c) 2018 小骥伏枥,志在万里
*
* @ClassName: AspectService.java
* @Description: 日志记录
*
* @version: v1.0.0
* @author: kanwj
* @date: 2018年11月29日 下午8:09:32
*
*/
public class AspectService {
private final static Log log = LogFactory.getLog(AspectService.class);
// 配置抛出异常后通知,使用在方法aspect()上注册的切入点
public void afterThrow(JoinPoint joinPoint, Exception ex) {
// 判断日志输出级别
if (log.isInfoEnabled()) {
log.info("afterThrow " + joinPoint + "\t" + ex.getMessage());
}
// 详细错误信息
String errorMsg = "";
StackTraceElement[] trace = ex.getStackTrace();
for (StackTraceElement s : trace) {
errorMsg += "\tat " + s + "\r\n";
}
// 写入异常日志
writeLog(errorMsg, joinPoint, ex);
}
/**
* 写日志
*
* @param detailErrMsg
* 详细错误信息
* @param joinPoint
* @param ex
*/
public void writeLog(String detailErrMsg, JoinPoint joinPoint, Exception ex) {
// 获取服务器路径
String path = System.getProperty("catalina.home") + "/logs_project/";
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();
// 获取请求的URL
StringBuffer requestURL = request.getRequestURL();
// 获取参 数信息
String queryString = request.getQueryString();
// 封装完整请求URL带参数
if (queryString != null) {
requestURL.append("?").append(queryString);
}
String cla = joinPoint.getTarget().getClass().getName();// action
String method = joinPoint.getSignature().getName();// method
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
// 创建输出异常log日志
String fpath = path + sdf1.format(new Date()) + "_error.log";
File file = new File(fpath);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream out = new FileOutputStream(file, true); // 如果追加方式用true
// 日志具体参数
StringBuffer sb = new StringBuffer();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sb.append("-----------" + sdf.format(new Date()) + "------------\r\n");
sb.append("远程请求URL[" + requestURL + "]\r\n");
sb.append("接口方法:[" + cla + "." + method + "]\r\n");
sb.append("详细错误信息:" + ex + "\r\n");
sb.append(detailErrMsg + "\r\n");
sb.append("\r\n\r\n\r\n");
out.write(sb.toString().getBytes("utf-8"));// 注意需要转换对应的字符集
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文参考文章: