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
添加回答
舉報