Spring MVC拦截器 log4j打印日志问题


学习Spring MVC不久,做了个log4j的小测试:想打印出项目中的重要错误,这个错误是不可预见的。于是,做了个Spring 拦截器,然后在拦截器afterCompletion方法中,使用logger.error()方法。但经过几次测试后发现,log4j在error级别无法打印出错误,要调到info级别才能打印出来错误,可是调到info级别,日志中又会产生很多我不想打印的信息。这下我该如何去解决好呢?求高人指点……谢谢!
以下为log4j配置代码:

   
  #\u914D\u7F6E
  
########\u6839\u8BB0\u5F55\u3002\u9ED8\u8BA4\u8BB0\u5F55DEBUG\u7EA7\u522B\uFF0C\u8F93\u51FA\u5230o1\u548Co2
log4j.rootLogger = error,o1,o2
#\u63A7\u5236\u53F0\u8F93\u51FA\u5668
log4j.appender.o1 = org.apache.log4j.ConsoleAppender
#\u5B9A\u4E49\u6837\u5F0F
log4j.appender.o1.layout = org.apache.log4j.PatternLayout
log4j.appender.o1.layout.ConversionPattern=%r [%t] [%p] - %c -%l -%m%n
#\u6587\u4EF6\u8F93\u51FA
log4j.appender.o2 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.o2.File= E:/log4j.txt
log4j.appender.o2.DatePattern='.'yyyy-MM-dd
log4j.appender.o2.layout.ConversionPattern=[%d{yyy-MM-dd HH:mm:ss}][%C-%M] -%m%n
log4j.appender.o2.layout=org.apache.log4j.PatternLayout

以下为Spring拦截器代码:

   
  package com.ticket.interceptor;
  

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.ticket.action.m.ManagerController;
import com.ticket.model.Business;
import com.ticket.model.Manager;
import com.ticket.model.View;

public class ControllerInterceptor extends HandlerInterceptorAdapter {

static Logger logger = Logger.getLogger(ControllerInterceptor.class);

private String mappingURL;

public String getMappingURL() {
return mappingURL;
}

public void setMappingURL(String mappingURL) {
this.mappingURL = mappingURL;
}

/**
* 在Controller方法前进行拦截
*/
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {


HttpSession session=request.getSession();
String url=request.getRequestURL().toString();
if(url!=null){
String url1="";
if(url.length()>1){
url1=url.substring(url.length()-2, url.length());
}
if(url.indexOf("/b/")>0||url1.equals("/b")){

Business user = (Business) session.getAttribute("buser");
if(user==null){
request.getRequestDispatcher("/index.jsp").forward(request, response);
return false;
}

}
if(url.indexOf("/m/")>0||url1.equals("/m")){

Manager user = (Manager) session.getAttribute("muser");
if(user==null){
request.getRequestDispatcher("/index.jsp").forward(request, response);
return false;
}

}
if(url.indexOf("/v/")>0||url1.equals("/v")){

View user = (View) session.getAttribute("vuser");
if(user==null){
request.getRequestDispatcher("/index.jsp").forward(request, response);
return false;
}
}
}

return true;
}

/**
* This implementation is empty.
*/
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
//System.out.println("ControllerInterceptor.postHandle()");
}

/**
* 在Controller方法后进行拦截
*/
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// System.out.println("ControllerInterceptor.afterCompletion()");
if(null!=ex){
logger.error("error:"+ex);
}
}
}

springMVC java

小小外星人 10 years, 11 months ago

Your Answer