![](/img/trans.png)
[英]How to configure spring-boot to serve static files using springmvc-router
[英]Enable logging in SpringMVC,not using Spring-boot
我正在創建一個簡單的 SpringMVC 項目,並希望記錄所有傳入的請求,包括 uri/query/payload/clientIp... ,所有信息。
我能找到的所有資源都是關於 Spring Boot,例如: https://www.javadevjournal.com/spring/log-incoming-requests-spring/
我還閱讀了官方文檔並找到了“enableLoggingRequestDetails”選項,但沒有關於它的詳細信息,我嘗試過但沒有工作。 https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-logging
那么,如何在純 SpringMVC 框架中實現這一點(沒有 Spring Boot)?
是否有 SpringMVC 內置的方法來做到這一點?
1.您可以實現一個過濾器class 從OncePerRequestFilter擴展,然后每個請求將通過您的過濾器 go。 然后您可以在此 class 中記錄您想要的內容。
@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擴展而來的攔截器class 。
@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>
好吧,為了在 Spring 應用程序中啟用日志記錄,您只需要
日志庫及其屬性文件
To make Log4j work with the default JCL dependency (commons-logging) all you need to do is put Log4j on the classpath, and provide it with a configuration file (log4j.properties or log4j.xml in the root of the classpath).
<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
使用攔截器你可以實現這個。檢查下面的鏈接,你會明白的
spring-mvc-4-how-to-intercept-request-with-a-handlerinterceptor
編輯:
使用CommonsRequestLoggingFilter
Spring MVC 提供了 CommonsRequestLoggingFilter 可以記錄請求 URL、body 等相關信息。
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.client = true
logging.query = true
logging.payload = true
logging.headers = true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.