3 回答
TA贡献1773条经验 获得超3个赞
1.您可以实现一个从OncePerRequestFilter扩展的Filter类,然后每个请求都会经过您的过滤器。然后你可以在这个类中记录你想要的内容。
@Slf4j
@Component
public class RequestLoggingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
log.debug(
String.format("FILTERED URL: %s", request.getRequestURI())
);
//continue filtering
filterChain.doFilter(request, response);
}
}
2.另一种方法是实现一个继承自HandlerInterceptorAdapter的 Interceptor 类。
@Slf4j
@Component
public class RequestLoggingHandler extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.debug(
String.format("HANDLER(pre) URL: %s", request.getRequestURI())
);
return super.preHandle(request, response, handler);
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.debug(
String.format("HANDLER(post) URL: %s", request.getRequestURI())
);
super.postHandle(request, response, handler, modelAndView);
}
}
但是您必须通过显式配置添加它来启用此拦截器。
@Configuration
public class WebApplicationConfiguration implements WebMvcConfigurer {
@Setter(onMethod_ = @Autowired)
private RequestLoggingHandler requestLogger;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestLogger);
}
}
3.另一种方法是使用标准CommonsRequestLoggingFilter。
这样你应该使用如下配置来配置它:
@Configuration
public class RequestLoggingFilterConfig {
@Bean
public CommonsRequestLoggingFilter logFilter() {
CommonsRequestLoggingFilter filter
= new CommonsRequestLoggingFilter();
filter.setIncludeQueryString(true);
filter.setIncludePayload(true);
filter.setMaxPayloadLength(10000);
filter.setIncludeHeaders(false);
filter.setAfterMessagePrefix("REQUEST DATA : ");
return filter;
}
}
然后在 logback.xml 中启用它:
<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
TA贡献1828条经验 获得超3个赞
使用CommonsRequestLoggingFilter
Spring MVC 提供了 CommonsRequestLoggingFilter ,可以记录请求 URL、正文等相关信息。
Commons请求日志过滤器
LoggingConfig.java
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
@Configuration
@PropertySource("classpath:logging.properties")
public class LoggingConfig {
@Autowired
private Environment env;
public CommonsRequestLoggingFilter requestLoggingFilter() {
CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
loggingFilter.setIncludeClientInfo(env.getProperty("logging.client"));
loggingFilter.setIncludeQueryString(env.getProperty("logging.query"));
loggingFilter.setIncludePayload(env.getProperty("logging.payload"));
loggingFilter.setIncludeHeaders(env.getProperty("logging.headers"));
return loggingFilter;
}
@Bean
public CommonsRequestLoggingFilter springLogging() {
Logger logger = (Logger) LoggerFactory.getLogger(CommonsRequestLoggingFilter.class);
logger.setLevel(Level.DEBUG);
return requestLoggingFilter();
}
}
logging.properties
logging.client = true
logging.query = true
logging.payload = true
logging.headers = true
TA贡献1793条经验 获得超6个赞
为了在 Spring 应用程序中启用日志记录,您需要做的是
日志库及其属性文件
要使 Log4j 使用默认的 JCL 依赖项(commons-logging),您需要做的就是将 Log4j 放在类路径上,并为其提供配置文件(类路径根目录中的 log4j.properties 或 log4j.xml)。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
<scope>runtime</scope>
</dependency>
log4j.properties
log4j.rootCategory=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
log4j.category.org.springframework.beans.factory=DEBUG
添加回答
举报