/**
* 日志拦截,查看执行耗时
*
* @author admin
*/
public class LogInterceptor implements Interceptor {
@Override
public void intercept(Invocation inv) {
long start = System.currentTimeMillis();
try {
inv.invoke();
} finally {
long usedTime = System.currentTimeMillis() - start;
//遍历出所有参数,数组的话用逗号分开显示所有参数
String args = "";
Map<String, String[]> parameterMap = (((BaseController) inv.getTarget()).getRequest()).getParameterMap();
for (String s : parameterMap.keySet()) {
args += s + "=";
for (String value : parameterMap.get(s)) {
args += value + ",";
}
args = args.substring(0, args.length() - 1) + ";";
}
if (StrKit.notBlank(args)) {
args = args.substring(0, args.length() - 1);
}
System.out.println("--------------------------------------------------------------------------------");
// 请求URL
System.out.println("Url : " + inv.getActionKey());
// 请求控制层全名
System.out.println("Controller : " + inv.getController());
// 请求方法名
System.out.println("Method : " + inv.getMethodName());
// 参数非空则显示
if (StrKit.notBlank(args)) {
System.out.println("Parameter : " + args);
}
// 方法执行耗时
System.out.println("TimeConsuming : " + usedTime + "ms");
System.out.println("--------------------------------------------------------------------------------");
}
}
}
实现效果:
--------------------------------------------------------------------------------
Url : /generator/code/getTemplatesContents
Controller : com.gen.portal.generator.controller.CodeController@26c51925
Method : getTemplatesContents
Parameter : title=_all_code
TimeConsuming : 21ms
--------------------------------------------------------------------------------