struts2配合log4j打印异常栈信息

关于配置struts2全局异常后控制台无法打印异常信息问题

Java代码 复制代码 收藏代码
  1. <global-results> 
  2.       <result name="error">/exception/error.jsp</result> 
  3.   </global-results> 
  4.    
  5.   <global-exception-mappings> 
  6.      <exception-mapping exception="java.lang.Exceptions" result="error" /> 
  7.      <exception-mapping result="error" exception="java.lang.Throwable"></exception-mapping>  
  8.   </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> 
  </global-exception-mappings>

配置完后,action中不try catch则无法在控制台打印异常信息。

多次尝试后解决方案如下:

在struts2中的defaultStack拦截器中配置参数如下

Java代码 复制代码 收藏代码
  1. <interceptor-ref name="defaultStack"
  2.     <param name="exception.logEnabled">true</param> 
  3.     <param name="exception.logLevel">error</param> 
  4. </interceptor-ref> 
<interceptor-ref name="defaultStack">
	<param name="exception.logEnabled">true</param>
	<param name="exception.logLevel">error</param>
</interceptor-ref>

再配合log4j中设置

Java代码 复制代码 收藏代码
  1. log4j.logger.com.opensymphony.xwork2=WARN  
log4j.logger.com.opensymphony.xwork2=WARN 

Java代码 复制代码 收藏代码
  1. log4j.logger.com.opensymphony.xwork2=ERROR 
log4j.logger.com.opensymphony.xwork2=ERROR

即可看到控制台打印异常信息,当然log4j中需要配置控制台输出。

原理:http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/interceptor/ExceptionMappingInterceptor.html

xwork中的ExceptionMappingInterceptor拦截器默认将异常打印关闭了。即默认只跳转到异常处理页。在开发时我们需要将其打开,即我们刚才struts中配置的参数.

该拦截器有三个参数。

Java代码 复制代码 收藏代码
  1. logEnabled (optional) - Should exceptions also be logged? (boolean true|false
  2. logLevel (optional) - what log level should we use (trace, debug, info, warn, error, fatal)? - defaut is debug 
  3. 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
logCategory (optional) - If provided we would use this category (eg. com.mycompany.app). Default is to use com.opensymphony.xwork.interceptor.ExceptionMappingInterceptor.

logEnabled配置是否打开日志输出

logLevel 配置拦截到异常的日志级别。

logCategory 应该是自定义日志。没怎么用,有需求的可以自己研究研究。有知道用途的给俺回复下啊。呵呵

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

Java代码 复制代码 收藏代码
  1. <interceptors> 
  2.             <interceptor name="authority" 
  3.                 class="com.test.SystemInterceptor" /> 
  4.             <interceptor-stack name="myAuth"
  5.                 <interceptor-ref name="defaultStack"
  6.                     <param name="exception.logEnabled">true</param> 
  7.                     <param name="exception.logLevel">error</param> 
  8.                 </interceptor-ref> 
  9.                 <interceptor-ref name="authority" /> 
  10.             </interceptor-stack> 
  11.         </interceptors> 
  12.         <default-interceptor-ref name="myAuth" /> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值