2017-12-19 14:55:52 +08:00
|
|
|
package com.nis.interceptor;
|
|
|
|
|
|
2018-06-08 15:42:59 +08:00
|
|
|
import java.io.BufferedReader;
|
2018-07-10 11:30:37 +08:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2018-06-08 15:42:59 +08:00
|
|
|
import java.io.IOException;
|
2018-07-10 11:30:37 +08:00
|
|
|
import java.io.InputStream;
|
2018-06-08 15:42:59 +08:00
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.nio.charset.Charset;
|
2017-12-19 14:55:52 +08:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
|
2018-06-08 15:42:59 +08:00
|
|
|
import javax.servlet.ServletInputStream;
|
|
|
|
|
import javax.servlet.ServletRequest;
|
2017-12-19 14:55:52 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.NamedThreadLocal;
|
|
|
|
|
import org.springframework.web.servlet.HandlerInterceptor;
|
|
|
|
|
import org.springframework.web.servlet.ModelAndView;
|
|
|
|
|
|
|
|
|
|
import com.nis.util.DateUtils;
|
|
|
|
|
import com.nis.util.LogUtils;
|
|
|
|
|
import com.nis.web.service.BaseService;
|
|
|
|
|
|
2018-06-08 15:42:59 +08:00
|
|
|
public class LogInterceptor extends BaseService implements HandlerInterceptor {
|
|
|
|
|
|
|
|
|
|
private static final ThreadLocal<Long> timeThreadLocal = new NamedThreadLocal<Long>("ThreadLocal StartTime");
|
|
|
|
|
|
2018-07-10 11:30:37 +08:00
|
|
|
/**
|
|
|
|
|
* 复制输入流
|
|
|
|
|
* @param inputStream
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static InputStream cloneInputStream(ServletInputStream inputStream) {
|
|
|
|
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
|
int len;
|
|
|
|
|
try {
|
|
|
|
|
while ((len = inputStream.read(buffer)) > -1) {
|
|
|
|
|
byteArrayOutputStream.write(buffer, 0, len);
|
|
|
|
|
}
|
|
|
|
|
byteArrayOutputStream.flush();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
InputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
|
|
|
|
|
return byteArrayInputStream;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-08 15:42:59 +08:00
|
|
|
/**
|
|
|
|
|
* 获取非get请求的参数
|
|
|
|
|
* @param request
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
private static String getBodyString(ServletRequest request) {
|
2018-07-10 11:30:37 +08:00
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
InputStream inputStream = null;
|
2018-06-08 15:42:59 +08:00
|
|
|
BufferedReader reader = null;
|
|
|
|
|
try {
|
2018-07-10 11:30:37 +08:00
|
|
|
inputStream = cloneInputStream(request.getInputStream());
|
2018-06-08 15:42:59 +08:00
|
|
|
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
|
|
|
|
|
String line = "";
|
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
sb.append(line);
|
|
|
|
|
}
|
2018-07-10 11:30:37 +08:00
|
|
|
} catch (IOException e) {
|
2018-06-08 15:42:59 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
|
} finally {
|
|
|
|
|
if (inputStream != null) {
|
|
|
|
|
try {
|
|
|
|
|
inputStream.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (reader != null) {
|
|
|
|
|
try {
|
|
|
|
|
reader.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return sb.toString();
|
|
|
|
|
}
|
2017-12-19 14:55:52 +08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
|
|
|
|
|
throws Exception {
|
2018-06-08 15:42:59 +08:00
|
|
|
String requestURI = request.getRequestURI();
|
2018-07-10 11:30:37 +08:00
|
|
|
// logger.info("请求路径是:" + requestURI);
|
|
|
|
|
// logger.info("请求参数是:" + getBodyString(request));
|
2018-06-08 15:42:59 +08:00
|
|
|
long beginTime = System.currentTimeMillis();// 1、开始时间
|
|
|
|
|
timeThreadLocal.set(beginTime); // 线程绑定变量(该数据只有当前请求的线程可见)
|
|
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
logger.debug("开始计时: {} URI: {}", new SimpleDateFormat("hh:mm:ss.SSS").format(beginTime),
|
|
|
|
|
request.getRequestURI());
|
2017-12-19 14:55:52 +08:00
|
|
|
}
|
2018-06-08 15:42:59 +08:00
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
|
|
|
ModelAndView modelAndView) throws Exception {
|
2018-06-08 15:42:59 +08:00
|
|
|
if (modelAndView != null) {
|
2017-12-19 14:55:52 +08:00
|
|
|
logger.info("ViewName: " + modelAndView.getViewName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
|
|
|
|
|
throws Exception {
|
2018-06-08 15:42:59 +08:00
|
|
|
long beginTime = timeThreadLocal.get();// 得到线程绑定的局部变量(开始时间)
|
|
|
|
|
long endTime = System.currentTimeMillis(); // 2、结束时间
|
|
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
long consumerTime = endTime - beginTime;
|
2018-06-08 15:42:59 +08:00
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
// 打印JVM信息。
|
2018-06-08 15:42:59 +08:00
|
|
|
if (logger.isDebugEnabled()) {
|
|
|
|
|
|
|
|
|
|
Object[] params = new Object[] { new SimpleDateFormat("hh:mm:ss.SSS").format(endTime),
|
|
|
|
|
DateUtils.formatDateTime(consumerTime), request.getRequestURI(),
|
|
|
|
|
Runtime.getRuntime().maxMemory() / 1024 / 1024, Runtime.getRuntime().totalMemory() / 1024 / 1024,
|
|
|
|
|
Runtime.getRuntime().freeMemory() / 1024 / 1024, (Runtime.getRuntime().maxMemory()
|
|
|
|
|
- Runtime.getRuntime().totalMemory() + Runtime.getRuntime().freeMemory()) / 1024 / 1024 };
|
|
|
|
|
logger.debug("计时结束:{} 耗时:{} URI: {} 最大内存: {}m 已分配内存: {}m 已分配内存中的剩余空间: {}m 最大可用内存: {}m", params);
|
|
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
}
|
2018-06-08 15:42:59 +08:00
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
// 保存日志
|
|
|
|
|
LogUtils.saveLog(request, handler, ex, null, consumerTime);
|
2018-06-08 15:42:59 +08:00
|
|
|
|
2017-12-19 14:55:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|