zoukankan      html  css  js  c++  java
  • 使用Spring进行统一日志管理 + 统一异常管理

    统一日志和异常管理配置好后,SSH项目中,代码以往散落的log.info() 和 try..catch..finally 再也不见踪影!

    统一日志异常实现类:

    [java] view plain copy
     
    1. package com.pilelot.web.util;  
    2.   
    3. import org.apache.log4j.Logger;  
    4. import org.springframework.aop.ThrowsAdvice;  
    5. import org.springframework.dao.DataAccessException;  
    6.   
    7. import java.io.IOException;  
    8. import java.lang.reflect.Method;  
    9. import java.sql.SQLException;  
    10.   
    11. /** 
    12.  * 由Spring AOP调用 输出异常信息,把程序异常抛向业务异常 
    13.  *  
    14.  * @author Andy Chan 
    15.  * 
    16.  */  
    17. public class ExceptionAdvisor implements ThrowsAdvice  
    18. {  
    19.     public void afterThrowing(Method method, Object[] args, Object target,  
    20.             Exception ex) throws Throwable  
    21.     {  
    22.         // 在后台中输出错误异常异常信息,通过log4j输出。  
    23.         Logger log = Logger.getLogger(target.getClass());  
    24.         log.info("**************************************************************");  
    25.         log.info("Error happened in class: " + target.getClass().getName());  
    26.         log.info("Error happened in method: " + method.getName());  
    27.             for (int i = 0; i < args.length; i++)  
    28.             {  
    29.                 log.info("arg[" + i + "]: " + args[i]);  
    30.             }  
    31.         log.info("Exception class: " + ex.getClass().getName());  
    32.         log.info("ex.getMessage():" + ex.getMessage());  
    33.         ex.printStackTrace();  
    34.         log.info("**************************************************************");  
    35.   
    36.         // 在这里判断异常,根据不同的异常返回错误。  
    37.         if (ex.getClass().equals(DataAccessException.class))  
    38.         {  
    39.             ex.printStackTrace();  
    40.             throw new BusinessException("数据库操作失败!");  
    41.         } else if (ex.getClass().toString().equals(  
    42.                 NullPointerException.class.toString()))  
    43.         {  
    44.             ex.printStackTrace();  
    45.             throw new BusinessException("调用了未经初始化的对象或者是不存在的对象!");  
    46.         } else if (ex.getClass().equals(IOException.class))  
    47.         {  
    48.             ex.printStackTrace();  
    49.             throw new BusinessException("IO异常!");  
    50.         } else if (ex.getClass().equals(ClassNotFoundException.class))  
    51.         {  
    52.             ex.printStackTrace();  
    53.             throw new BusinessException("指定的类不存在!");  
    54.         } else if (ex.getClass().equals(ArithmeticException.class))  
    55.         {  
    56.             ex.printStackTrace();  
    57.             throw new BusinessException("数学运算异常!");  
    58.         } else if (ex.getClass().equals(ArrayIndexOutOfBoundsException.class))  
    59.         {  
    60.             ex.printStackTrace();  
    61.             throw new BusinessException("数组下标越界!");  
    62.         } else if (ex.getClass().equals(IllegalArgumentException.class))  
    63.         {  
    64.             ex.printStackTrace();  
    65.             throw new BusinessException("方法的参数错误!");  
    66.         } else if (ex.getClass().equals(ClassCastException.class))  
    67.         {  
    68.             ex.printStackTrace();  
    69.             throw new BusinessException("类型强制转换错误!");  
    70.         } else if (ex.getClass().equals(SecurityException.class))  
    71.         {  
    72.             ex.printStackTrace();  
    73.             throw new BusinessException("违背安全原则异常!");  
    74.         } else if (ex.getClass().equals(SQLException.class))  
    75.         {  
    76.             ex.printStackTrace();  
    77.             throw new BusinessException("操作数据库异常!");  
    78.         } else if (ex.getClass().equals(NoSuchMethodError.class))  
    79.         {  
    80.             ex.printStackTrace();  
    81.             throw new BusinessException("方法末找到异常!");  
    82.         } else if (ex.getClass().equals(InternalError.class))  
    83.         {  
    84.             ex.printStackTrace();  
    85.             throw new BusinessException("Java虚拟机发生了内部错误");  
    86.         } else  
    87.         {  
    88.             ex.printStackTrace();  
    89.             throw new BusinessException("程序内部错误,操作失败!" + ex.getMessage());  
    90.         }  
    91.     }  
    92. }  


    自定义业务异常处理类 友好提示:

    [java] view plain copy
     
    1. package com.pilelot.web.util;  
    2.   
    3. /** 
    4.  * 自定义业务异常处理类    友好提示 
    5.  * @author Andy Chan 
    6.  * 
    7.  */  
    8. public class BusinessException extends RuntimeException  
    9. {  
    10.     private static final long serialVersionUID = 3152616724785436891L;  
    11.   
    12.     public BusinessException(String frdMessage)  
    13.     {  
    14.         super(createFriendlyErrMsg(frdMessage));  
    15.     }  
    16.   
    17.     public BusinessException(Throwable throwable)  
    18.     {  
    19.         super(throwable);  
    20.     }  
    21.   
    22.     public BusinessException(Throwable throwable, String frdMessage)  
    23.     {  
    24.         super(throwable);  
    25.     }  
    26.   
    27.     private static String createFriendlyErrMsg(String msgBody)  
    28.     {  
    29.         String prefixStr = "抱歉,";  
    30.         String suffixStr = " 请稍后再试或与管理员联系!";  
    31.   
    32.         StringBuffer friendlyErrMsg = new StringBuffer("");  
    33.   
    34.         friendlyErrMsg.append(prefixStr);  
    35.   
    36.         friendlyErrMsg.append(msgBody);  
    37.   
    38.         friendlyErrMsg.append(suffixStr);  
    39.   
    40.         return friendlyErrMsg.toString();  
    41.     }  
    42. }  


     

    统一日志处理实现类:

    [java] view plain copy
     
    1. package com.pilelot.web.util;  
    2.   
    3. import org.aopalliance.intercept.MethodInterceptor;  
    4. import org.aopalliance.intercept.MethodInvocation;  
    5. import org.apache.log4j.Logger;  
    6.   
    7. /** 
    8.  * Spring 统一日志处理实现类 
    9.  * @author Andy Chan 
    10.  *  
    11.  */  
    12. public class LogInterceptor implements MethodInterceptor  
    13. {  
    14.   
    15.     public Object invoke(MethodInvocation invocation) throws Throwable  
    16.     {  
    17.         Logger loger = Logger.getLogger(invocation.getClass());  
    18.   
    19.         loger.info("--Log By Andy Chan -----------------------------------------------------------------------------");  
    20.         loger.info(invocation.getMethod() + ":BEGIN!--(Andy ChanLOG)");// 方法前的操作  
    21.         Object obj = invocation.proceed();// 执行需要Log的方法  
    22.         loger.info(invocation.getMethod() + ":END!--(Andy ChanLOG)");// 方法后的操作  
    23.         loger.info("-------------------------------------------------------------------------------------------------");  
    24.   
    25.         return obj;  
    26.     }  
    27.   
    28. }  

    Spring配置文件添加:

    [html] view plain copy
     
    1. <!-- Spring 统一日志处理   LogInterceptor拦截器 配置 -->     
    2. <bean id="logLnterceptor" class="com.pilelot.web.util.LogInterceptor"/>  
    3. <!-- Spring 统一异常处理  ExceptionAdvisor配置 -->  
    4. <bean id="exceptionHandler" class="com.pilelot.web.util.ExceptionAdvisor"></bean>  
    5.   
    6.     <!-- Bean自动代理处理器 配置-->    
    7. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator" >  
    8.    <property name="beanNames">  
    9.     <list>    <!-- 配置需要进行日志记录的Service和Dao -->  
    10.         <value>commonDao</value>  
    11.                 <!-- 配置所有Service结尾命名的Bean,即所有Service层的类都要经过exceptionHandler异常处理类 -->   
    12.         <value>*Service</value>  <!-- Service层的Bean ID 命名要以Service结尾 -->  
    13.     </list>  
    14.    </property>  
    15.    <property name="interceptorNames">  
    16.     <list>  
    17.          <value>exceptionHandler</value>  
    18.          <value>logLnterceptor</value>  
    19.          <!--<value>transactionInterceptor</value>-->  
    20.     </list>  
    21.    </property>  
    22. </bean>  
    23. lt;!-- ——————————————————Spring 统一日志处理 + 统一异常处理  配置结束—————————————悲伤的分隔线—————————— -->  




    这样简单三步,就实现了由Spring统一日志+统一异常管理,代码清爽了不少!

  • 相关阅读:
    asp.net core3.1修改代码以支持windows服务发布
    .NET Core3.1IFreeSql使用FreeSql.Generator自动生成实体类
    西南大学2021年秋《线性代数》参考答案
    西南大学2021年秋形势与政策2 阶段一参考答案
    送给程序员的礼物
    西南大学2021年秋形势与政策2 阶段三参考答案
    拿 M 奖真的那么难吗?这份美赛攻略请收下!
    前端开发必会实战项目,Vue Router 实战来啦!​​
    你喜欢哪款 Linux 桌面?萝莉风?御姐风?
    Python 挑战,你通关了吗?速来看题解!
  • 原文地址:https://www.cnblogs.com/exmyth/p/5689632.html
Copyright © 2011-2022 走看看