亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 SpringMVC 中啟用日志記錄,而不是使用 Spring-boot

在 SpringMVC 中啟用日志記錄,而不是使用 Spring-boot

茅侃侃 2023-09-13 10:22:09
我正在創建一個簡單的 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 是否有內置方法可以做到這一點?
查看完整描述

3 回答

?
慕容3067478

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>


查看完整回答
反對 回復 2023-09-13
?
倚天杖

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


查看完整回答
反對 回復 2023-09-13
?
搖曳的薔薇

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


查看完整回答
反對 回復 2023-09-13
  • 3 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號