<interceptor-ref name="defaultStack"> <param name="exception.logEnabled">true</param> <param name="exception.logLevel">error</param> </interceptor-ref>
log4j.properties中加下如下配置:
log4j.logger.com.opensymphony.xwork2=WARN log4j.logger.com.opensymphony.xwork2=ERROR
struts2配合log4j打印异常栈信息
关于配置struts2全局异常后控制台无法打印异常信息问题
- <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>
- </global-exception-mappings>
配置完后,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>
- </interceptor-ref>
再配合log4j中设置
- log4j.logger.com.opensymphony.xwork2=WARN
- log4j.logger.com.opensymphony.xwork2=WARN
或
- log4j.logger.com.opensymphony.xwork2=ERROR
- 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
- 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 应该是自定义日志。没怎么用,有需求的可以自己研究研究。有知道用途的给俺回复下啊。呵呵
----------------------------------------------------------------------------------
- <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" />