jfinal统一的异常及日志处理的拦截器
2014-04-04 11:29
579 查看
摘要: 一个jfinal统一的异常及日志处理的拦截器,实现对异常拦截自动判断是否ajax请求从而采取不同的错误输出方式,另外通过根据配置对指定url进行行为日志记录
分享一个jfinal统一的异常及日志处理的拦截器
直接上代码吧
分享一个jfinal统一的异常及日志处理的拦截器
直接上代码吧
package com.smhaochi.web.interceptor; import java.util.Date; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import com.smhaochi.exception.BaseBussException; import com.smhaochi.model.ActionLog; import com.smhaochi.model.Menu; import com.smhaochi.vo.ActionLogVo; import com.smhaochi.web.controller.BaseController; import com.jfinal.aop.Interceptor; import com.jfinal.core.ActionInvocation; import com.jfinal.core.JFinal; import com.jfinal.log.Logger; /** * @title: 日志/异常处理拦截器 * @className: ActionLogInterceptor * @description: TODO * @company: FOREVEROSS * @author: <a href="http://www.smhaochi.com">vakin jiang</a> * @createDate: 2014年4月2日 * @version: 1.0 */ public class ExceptionAndLogInterceptor implements Interceptor { private static final Logger log = Logger.getLogger(ExceptionAndLogInterceptor.class); @Override public void intercept(ActionInvocation ai) { BaseController controller = (BaseController)ai.getController(); HttpServletRequest request = controller.getRequest(); boolean successed = false; try { ai.invoke(); successed = true; } catch (Exception e) { // doLog(ai,e); //判断是否ajax请求 String header = request.getHeader("X-Requested-With"); boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header); String msg = formatException(e); if(isAjax){ msg = new StringBuilder().append("{\"status\":\"0\",\"msg\":\"") .append(msg).append("\"}").toString(); controller.renderJson(msg); }else{ String redirctUrl = request.getHeader("referer"); if(StringUtils.isBlank(redirctUrl)){ redirctUrl = request.getRequestURI(); } controller.setAttr("message", msg); controller.setAttr("redirctUrl",redirctUrl); controller.render("../public/failed.ftl"); } }finally{ //记录日志 try { Menu menu = matchDefineRecordLogMenu(request); if(menu != null){ ActionLogVo actionLog = controller.wrapActionLog(); new ActionLog().set(ActionLog.ACT_NAME, menu.getName())// .set(ActionLog.ACT_TIME, new Date())// .set(ActionLog.DATA, actionLog.getData())// .set(ActionLog.IP, actionLog.getIp())// .set(ActionLog.SUCESSED, successed)// .set(ActionLog.URI, actionLog.getUri())// .set(ActionLog.USER_ID, actionLog.getUserId())// .set(ActionLog.USER_NAME, actionLog.getUserName())// .save(); } } catch (Exception e2) {} } } /** * @methodName: matchDefineRecordLogMenu * @description: 匹配已定义需要记录日志的菜单 * @author: vakinge * @createDate: 2014年4月3日 * @param request * @return */ private Menu matchDefineRecordLogMenu(HttpServletRequest request) { Map<String, Menu> menus = Menu.getAllUrlMenus(); if(menus != null){ String uri = request.getRequestURI(); Set<String> urls = menus.keySet(); for (String url : urls) { if(url == null)continue; //url匹配 && 有参数 && 已配置记录日志 if(url.contains(uri) && request.getParameterNames().hasMoreElements() && menus.get(url).isRecordLog()){ return menus.get(url); } } } return null; } /** * @methodName: doLog * @description: 记录log4j日志 * @author: vakinge * @createDate: 2014年4月3日 * @param ai * @param e */ private void doLog(ActionInvocation ai,Exception e) { //开发模式 if(JFinal.me().getConstants().getDevMode()){ e.printStackTrace(); } //业务异常不记录 ?? if( e instanceof BaseBussException)return; StringBuilder sb =new StringBuilder("\n---Exception Log Begin---\n"); sb.append("Controller:").append(ai.getController().getClass().getName()).append("\n"); sb.append("Method:").append(ai.getMethodName()).append("\n"); sb.append("Exception Type:").append(e.getClass().getName()).append("\n"); sb.append("Exception Details:"); log.error(sb.toString(), e); } /** * 格式化异常信息,用于友好响应用户 * @param e * @return */ private static String formatException(Exception e){ String message = null; Throwable ourCause = e; while ((ourCause = e.getCause()) != null) { e = (Exception) ourCause; } String eClassName = e.getClass().getName(); //一些常见异常提示 if("java.lang.NumberFormatException".equals(eClassName)){ message = "请输入正确的数字"; }else if (e instanceof BaseBussException || e instanceof RuntimeException) { message = e.getMessage(); if(StringUtils.isBlank(message))message = e.toString(); } //获取默认异常提示 if (StringUtils.isBlank(message)){ message = "系统繁忙,请稍后再试"; } //替换特殊字符 message = message.replaceAll("\"", "'"); return message; } }
相关文章推荐
- 利用spring aop统一处理异常和打日志
- 利用spring aop统一处理异常和打日志
- SpringMVC异常统一处理(异常信息的国际化,日志记录)
- 【SpringBoot】web进阶——表单验证,AOP统一处理请求日志,统一异常处理,单元测试
- springMVC拦截器、json转换器、统一异常处理在配置文件中配置
- Spring MVC异常统一处理(异常信息的国际化,日志记录)
- Spring MVC异常统一处理(异常信息的国际化,日志记录)
- Extending WCF(四)—一个统一处理异常、日志的解决方案
- Extending WCF(四)—一个统一处理异常、日志的解决方案
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- 使用Spring.NET统一ASP.NET异常处理并记录日志
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
- Okhttp拦截器统一异常处理并多次读取response.body().string()
- SpringMVC【校验器、统一处理异常、RESTful、拦截器】
- STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)
- Spring MVC自定义统一异常处理类,并且在控制台中输出错误日志
- Spring 集成 AOP 实现统一处理请求日志
- 使用Spring实现异常统一处理【一】
- springmvc异常统一处理