使用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統一日志+統一異常管理,代碼清爽了不少!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM