天天看點

Struts2 控制台不列印異常的解決方案

struts2 控制台不列印異常解決方法

 log4j.properties中加下如下配置:

struts2配合log4j列印異常棧資訊

關于配置struts2全局異常後控制台無法列印異常資訊問題

java代碼

<global-results>   

      <result name="error">/exception/error.jsp</result>   

  </global-results>   

  <global-exception-mappings>   

     <exception-mapping exception="java.lang.exceptions" result="error" />   

     <exception-mapping result="error" exception="java.lang.throwable"></exception-mapping>    

  </global-exception-mappings>  

<global-results>  

      <result name="error">/exception/error.jsp</result>  

  </global-results>  

  <global-exception-mappings>  

     <exception-mapping exception="java.lang.exceptions" result="error" />  

     <exception-mapping result="error" exception="java.lang.throwable"></exception-mapping>   

配置完後,action中不try catch則無法在控制台列印異常資訊。

多次嘗試後解決方案如下:

在struts2中的defaultstack攔截器中配置參數如下

<interceptor-ref name="defaultstack">   

    <param name="exception.logenabled">true</param>   

    <param name="exception.loglevel">error</param>   

</interceptor-ref>  

<interceptor-ref name="defaultstack">  

    <param name="exception.logenabled">true</param>  

    <param name="exception.loglevel">error</param>  

 再配合log4j中設定

log4j.logger.com.opensymphony.xwork2=warn   

 或

log4j.logger.com.opensymphony.xwork2=error  

即可看到控制台列印異常資訊,當然log4j中需要配置控制台輸出。

xwork中的exceptionmappinginterceptor攔截器預設将異常列印關閉了。即預設隻跳轉到異常處理頁。在開發時我們需要将其打開,即我們剛才struts中配置的參數.

該攔截器有三個參數。

logenabled (optional) - should exceptions also be logged? (boolean true|false)   

loglevel (optional) - what log level should we use (trace, debug, info, warn, error, fatal)? - defaut is debug   

logcategory (optional) - if provided we would use this category (eg. com.mycompany.app). default is to use com.opensymphony.xwork.interceptor.exceptionmappinginterceptor.  

logenabled (optional) - should exceptions also be logged? (boolean true|false)  

loglevel (optional) - what log level should we use (trace, debug, info, warn, error, fatal)? - defaut is debug  

logenabled配置是否打開日志輸出

loglevel 配置攔截到異常的日志級别。

logcategory 應該是自定義日志。沒怎麼用,有需求的可以自己研究研究。有知道用途的給俺回複下啊。呵呵

----------------------------------------------------------------------------------

<interceptors>   

            <interceptor name="authority"  

                class="com.test.systeminterceptor" />   

            <interceptor-stack name="myauth">   

                <interceptor-ref name="defaultstack">   

                    <param name="exception.logenabled">true</param>   

                    <param name="exception.loglevel">error</param>   

                </interceptor-ref>   

                <interceptor-ref name="authority" />   

            </interceptor-stack>   

        </interceptors>   

        <default-interceptor-ref name="myauth" />