文章转自:http://hi.baidu.com/wuhuijinshen/blog/item/4418d54577b40557510ffef0.html
我们写的系统在运行过程中会发生一些意想不到的bug,为了跟踪这些bug,常做的方式是使用try块,在捕获异常后把信息写入日志。假如有一个历史遗留系统,它没有做异常捕获日志,而现在需要加上日志,怎么办呢?
如果你了解AOP,你会知道不必重写try块。而struts2的拦截器实现原理就是AOP,strut2中提供了一个异常拦截器ExceptionMappingInterceptor,在发生指定异常后,会由它处理。从它的源码可以知道,这个类是有写日志的功能的,就是默认是禁用了,最快的实现就是直接启用它的日志功能。
Struts2异常配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--继承struts-default以便使用它的拦截器stack——defaultStack-->
<packagename="youname"extends="struts-default"namespace="/youurl">
<!--定义一个拦截器stack -->
<interceptors>
<interceptor-stackname="logException">
<interceptor-refname="defaultStack"/>
<!--覆盖defultStack中的exception设置,启用它的日志功能-->
<interceptor-refname="exception">
<paramname="logEnabled">true</param>
<paramname="logLevel">info</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!--修改默认的拦截器stack,原来是defaultStack -->
<default-interceptor-refname="logException"/>
<!--全局跳转-->
<global-results>
<resultname="exception">/500.jsp</result>
</global-results>
<!--定义要捕获的异常-->
<global-exception-mappings>
<exception-mappingresult="exception"exception="java.lang.Exception"/>
</global-exception-mappings>
</package>
</struts>
日志配置
在src中配置log4j.properties文件,略。
个性化日志输出
如果不想使用ExceptionMappingInterceptor中自带的日志输入方式,可以自己实现一个拦截器,然后在上面的<interceptors>元素前定义它,如:
<interceptor name="exception" class="com.yourInterceptor"/>
优缺点
这种方式可以记录没有处理的异常(包括没有捕获处理的运行时异常和已经捕获处理,但继续往上层抛出的异常),对于那些被捕获但不往上层抛出的异常,由于异常拦截器无法捕获,也就无法写日志。
我们写的系统在运行过程中会发生一些意想不到的bug,为了跟踪这些bug,常做的方式是使用try块,在捕获异常后把信息写入日志。假如有一个历史遗留系统,它没有做异常捕获日志,而现在需要加上日志,怎么办呢?
如果你了解AOP,你会知道不必重写try块。而struts2的拦截器实现原理就是AOP,strut2中提供了一个异常拦截器ExceptionMappingInterceptor,在发生指定异常后,会由它处理。从它的源码可以知道,这个类是有写日志的功能的,就是默认是禁用了,最快的实现就是直接启用它的日志功能。
Struts2异常配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstrutsPUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN""http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--继承struts-default以便使用它的拦截器stack——defaultStack-->
<packagename="youname"extends="struts-default"namespace="/youurl">
<!--定义一个拦截器stack -->
<interceptors>
<interceptor-stackname="logException">
<interceptor-refname="defaultStack"/>
<!--覆盖defultStack中的exception设置,启用它的日志功能-->
<interceptor-refname="exception">
<paramname="logEnabled">true</param>
<paramname="logLevel">info</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<!--修改默认的拦截器stack,原来是defaultStack -->
<default-interceptor-refname="logException"/>
<!--全局跳转-->
<global-results>
<resultname="exception">/500.jsp</result>
</global-results>
<!--定义要捕获的异常-->
<global-exception-mappings>
<exception-mappingresult="exception"exception="java.lang.Exception"/>
</global-exception-mappings>
</package>
</struts>
日志配置
在src中配置log4j.properties文件,略。
个性化日志输出
如果不想使用ExceptionMappingInterceptor中自带的日志输入方式,可以自己实现一个拦截器,然后在上面的<interceptors>元素前定义它,如:
<interceptor name="exception" class="com.yourInterceptor"/>
优缺点
这种方式可以记录没有处理的异常(包括没有捕获处理的运行时异常和已经捕获处理,但继续往上层抛出的异常),对于那些被捕获但不往上层抛出的异常,由于异常拦截器无法捕获,也就无法写日志。