使用Spring进行统一日志管理 + 统一异常管理
来源:互联网 发布:淘宝九块邮优站 编辑:程序博客网 时间:2024/04/28 00:29
统一日志和异常管理配置好后,SSH项目中代码以往散落的log.info() 和 try..catch..finally 再也不见踪影,统一日志异常实现类。
1,统一日志处理实现类
package net.xnzz.util;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;import org.apache.log4j.Logger;/** * Spring 统一日志处理实现类 * @author WANGJIE * */public class LogInterceptor implements MethodInterceptor {public Object invoke(MethodInvocation invocation) throws Throwable { Logger loger = Logger.getLogger(invocation.getClass()); loger.info("--Log By Andy Chan -----------------------------------------------------------------------------"); loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");//方法前的操作 Object obj = invocation.proceed();// 执行需要Log的方法 loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");//方法后的操作 loger.info("-------------------------------------------------------------------------------------------------"); return obj; }}2,自定义业务异常处理类
package net.xnzz.util;/** * 自定义业务异常处理类 友好提示 * @author WANGJIE * */public class BusinessException extends RuntimeException {private static final long serialVersionUID = 3152616724785436891L; public BusinessException(String frdMessage) { super(createFriendlyErrMsg(frdMessage)); } public BusinessException(Throwable throwable) { super(throwable); } public BusinessException(Throwable throwable, String frdMessage) { super(throwable); } private static String createFriendlyErrMsg(String msgBody) { String prefixStr = "抱歉"; String suffixStr = " 请稍后再试或与管理员联系"; StringBuffer friendlyErrMsg = new StringBuffer(""); friendlyErrMsg.append(prefixStr); friendlyErrMsg.append(msgBody); friendlyErrMsg.append(suffixStr); return friendlyErrMsg.toString(); } }
3,异常处理类
package net.xnzz.util;import java.io.IOException;import java.lang.reflect.Method;import java.sql.SQLException;import org.apache.log4j.Logger;import org.springframework.aop.ThrowsAdvice;import org.springframework.dao.DataAccessException;/** * 由Spring AOP调用 输出异常信息把程序异常抛向业务异常 * @author WANGJIE * */public class ExceptionAdvisor implements ThrowsAdvice {public void afterThrowing(Method method, Object[] args, Object target, Exception ex) throws Throwable { // 在后台中输出错误异常异常信息通过log4j输出。 Logger log = Logger.getLogger(target.getClass()); log.info("***********************************************************"); log.info("Error happened in class: " + target.getClass().getName()); log.info("Error happened in method: " + method.getName()); for (int i = 0; i < args.length; i++) { log.info("arg[" + i + "]: " + args[i]); } log.info("Exception class: " + ex.getClass().getName()); log.info("ex.getMessage():" + ex.getMessage()); ex.printStackTrace(); log.info("***********************************************************"); // 在这里判断异常根据不同的异常返回错误。 if (ex.getClass().equals(DataAccessException.class)) { ex.printStackTrace(); throw new BusinessException("数据库操作失败"); } else if (ex.getClass().toString().equals(NullPointerException.class.toString())) { ex.printStackTrace(); throw new BusinessException("调用了未经初始化的对象或者是不存在对象"); } else if (ex.getClass().equals(IOException.class)) { ex.printStackTrace(); throw new BusinessException("IO异常"); } else if (ex.getClass().equals(ClassNotFoundException.class)) { ex.printStackTrace(); throw new BusinessException("指定的类不存在"); } else if (ex.getClass().equals(ArithmeticException.class)) { ex.printStackTrace(); throw new BusinessException("数学运算异常"); } else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class)) { ex.printStackTrace(); throw new BusinessException("数组下标越界!"); } else if (ex.getClass().equals(IllegalArgumentException.class)) { ex.printStackTrace(); throw new BusinessException("方法的参数错误"); } else if (ex.getClass().equals(ClassCastException.class)) { ex.printStackTrace(); throw new BusinessException("类型强制转换错误"); } else if (ex.getClass().equals(SecurityException.class)) { ex.printStackTrace(); throw new BusinessException("违背安全原则异常"); } else if (ex.getClass().equals(SQLException.class)) { ex.printStackTrace(); throw new BusinessException("操作数据库异常"); } else if (ex.getClass().equals(NoSuchMethodError.class)) { ex.printStackTrace(); throw new BusinessException("方法末找到异常"); } else if (ex.getClass().equals(InternalError.class)) { ex.printStackTrace(); throw new BusinessException("Java虚拟机发生了内部错误"); } else { ex.printStackTrace(); throw new BusinessException("程序内部错误操作失败" + ex.getMessage()); } }}
4,Spring配置文件添加
<!-- Spring 统一日志处理 LogInterceptor拦截器 配置 --> <bean id="logLnterceptor" class="net.xnzz.util.LogInterceptor"/> <!-- Spring 统一异常处理 ExceptionAdvisor配置 --> <bean id="exceptionHandler" class="net.xnzz.util.ExceptionAdvisor"></bean> <!-- Bean自动代理处理器 配置--> <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" > <property name="beanNames"> <list> <!-- 配置需要进行日志记录的Service和Dao --> <value>*Dao</value> <!-- 配置所有Service结尾命名的Bean即所有Service层的类都要经过exceptionHandler异常处理类 --> <!--<value>*Service</value> Service层的Bean ID 命名要以Service结尾 --> </list> </property> <property name="interceptorNames"> <list> <value>exceptionHandler</value> <value>logLnterceptor</value> <!--<value>transactionInterceptor</value>--> </list> </property> </bean>
0 0
- 使用Spring进行统一日志管理 + 统一异常管理
- 使用Spring进行统一日志管理 + 统一异常管理
- 使用Spring进行统一日志管理 + 统一异常管理
- 使用Spring进行统一日志管理 + 统一异常管理
- Spring统一异常管理
- SSH 统一日志管理
- springmvc统一异常管理
- 使用Gradle进行统一依赖管理
- 使用Spring中的ExceptionHandlerExceptionResolver进行统一的异常处理
- 使用Spring中的ExceptionHandlerExceptionResolver进行统一的异常处理
- 基于log4j实现统一日志管理
- 基于log4j实现统一日志管理
- 统一日志管理-Mongo 篇
- ACEGI结合LDAP进行统一用户管理
- Spring统一异常处理
- Spring > 统一异常处理
- Spring统一异常处理
- 使用Spring MVC统一异常处理实战
- HDU 1385 Minimum Transport Cost (字典序打印最短路)
- 导入文件No resource found that matches the given name
- pragma pack
- 谈一谈,做一个杀毒软件,必备的Windows驱动程序
- sql 查询一个表的数据插入到别一个表,ID自动增长
- 使用Spring进行统一日志管理 + 统一异常管理
- 20141030 N2
- 独立开发者创业过程中会犯的14个错误
- css 简单梳理
- 在Matlab中显示Kinect设备跟踪到的骨骼数据
- 项目全局异常处理
- HDU 1978 How many ways (DP,计数)
- CCArray数组或者vector容器元素在遍历时删除的删除技巧
- Fedora下的Ogre编译与安装