log4j.rootLogger = debug,stdout,F
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.Encoding = Utf-8
log4j.appender.stdout.Threshold = debug
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n
log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.File =d:/log/1111111.txt
log4j.appender.F.Append = true
log4j.appender.F.Threshold = INFO
log4j.appender.F.MaxFileSize = 1
log4j.appender.F.MaxBackupIndex = 5
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.layout.ConversionPattern =[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n
log4j.logger.exeception.ExceptionAction=DEBUG, async
log4j.additivity.exeception.ExceptionAction=false
log4j.appender.async=org.apache.log4j.RollingFileAppender
log4j.appender.async.File=d:/logs/async.log
log4j.appender.async.Append=true
log4j.appender.async.MaxFileSize=1GB
log4j.appender.async.MaxBackupIndex=5
log4j.appender.async.layout=org.apache.log4j.PatternLayout
log4j.appender.async.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%-5p][%c{1}] [%t] - %m%n
log4j.appender.async.encoding=gbk
<!-- 测试自定义异常 -->
<action name="exceptionAction_*" class="exeception.ExceptionAction" method="{1}">
<result>/hello.jsp</result>
</action>
<package name="all" extends="struts-default">
<interceptors>
<interceptor name="error" class="interception.ErrorInterceptor"></interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myStacks">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="error" />
</interceptor-stack>
</interceptors>
<!-- 覆盖底层的拦截器栈 对包中的所有action都有效 -->
<default-interceptor-ref name="myStacks"></default-interceptor-ref>
<global-results>
<result name="error" type="dispatcher">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
</package>
package interception;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class ErrorInterceptor implements Interceptor {
public void init() {
}
public String intercept(ActionInvocation actioninvocation) {
String result = null; // Action的返回值
try {
// 运行被拦截的Action,期间如果发生异常会被catch住
result = actioninvocation.invoke();
return result;
} catch (Exception e) {
/**
* 处理异常
*/
String errorMsg = "出现错误信息,请查看日志!";
//通过instanceof判断到底是什么异常类型
if (e instanceof RuntimeException) {
//未知的运行时异常
RuntimeException re = (RuntimeException) e;
//re.printStackTrace();
errorMsg = re.getMessage().trim();
System.out.println(errorMsg);
}
//把自定义错误信息
HttpServletRequest request = (HttpServletRequest) actioninvocation
.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
/**
* 发送错误消息到页面
*/
request.setAttribute("errorMsg", errorMsg);
/**
* log4j记录日志
*/
Logger log = Logger.getLogger(actioninvocation.getAction().getClass());
log.error(errorMsg, e);
return "error";
}// ...end of catch
}
public void destroy() {
}
}
package exeception;
import com.opensymphony.xwork2.ActionSupport;
public class ExceptionAction extends ActionSupport{
public String testException() {
//throw new RuntimeException("hello exception interceptor!!!");
try {
int i=1/0;
} catch (Exception e) {
System.out.println("测试 11是否执行到这句!!!!");
throw new RuntimeException("出错了!!");
}
return "success";
}
}