自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot 記錄Controller接口請求日志七種方式,第六種性能極高

開發(fā) 前端
API接口記錄請求相關(guān)日志是確保系統(tǒng)安全性、可維護性以及性能監(jiān)控的重要措施之一。通過記錄API請求的詳細信息,包括請求時間、來源IP地址、請求方法(GET、POST等)、請求參數(shù)、響應(yīng)狀態(tài)碼及響應(yīng)時間等,開發(fā)者能夠有效追蹤錯誤源頭、分析用戶行為模式、優(yōu)化系統(tǒng)性能,并保障數(shù)據(jù)安全。

環(huán)境:SpringBoot3.2.5

1. 簡介

API接口記錄請求相關(guān)日志是確保系統(tǒng)安全性、可維護性以及性能監(jiān)控的重要措施之一。通過記錄API請求的詳細信息,包括請求時間、來源IP地址、請求方法(GET、POST等)、請求參數(shù)、響應(yīng)狀態(tài)碼及響應(yīng)時間等,開發(fā)者能夠有效追蹤錯誤源頭、分析用戶行為模式、優(yōu)化系統(tǒng)性能,并保障數(shù)據(jù)安全。

接下來,我將介紹七種API接口請求日志的記錄方式:

圖片圖片

準備接口

@RestController
@RequestMapping("/api")
public class ApiController {


  private final static Logger logger = LoggerFactory.getLogger(ApiController.class) ;
  
  @GetMapping("/query/{category}")
  public ResponseEntity<Object> query(@PathVariable String category, @RequestParam String keyword) {
    logger.info("查詢數(shù)據(jù), 分類: {}, 關(guān)鍵詞: {}", category, keyword) ;
    return ResponseEntity.ok("success") ;
  }
}

接下來的示例我們都將通過該接口進行測試。

2. 實戰(zhàn)案例

2.1 Filter過濾

通過Filter方式可以攔截并記錄HTTP請求和響應(yīng)的組件。通過Filter技術(shù),開發(fā)人員可以在請求到達目標資源之前或響應(yīng)返回客戶端之前進行日志記錄,從而實現(xiàn)訪問控制和請求預(yù)處理等功能。

如果你考慮使用Filter,那么非常幸運你不需要自己去實現(xiàn),Spring MVC給我們提供了此功能的實現(xiàn),如下類圖:

圖片圖片

這里我們可以直接注冊CommonRequestLoggingFilter過濾器。

@Bean
FilterRegistrationBean<Filter> loggingFilter() {
  FilterRegistrationBean<Filter> reg = new FilterRegistrationBean<>() ;
  CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter() ;
  // 是否記錄客戶端信息
  filter.setIncludeClientInfo(true) ;
  // 記錄請求header數(shù)據(jù)
  filter.setIncludeHeaders(true) ;
  // 記錄請求body內(nèi)容
  filter.setIncludePayload(true) ;
  // 是否記錄查詢字符串
  filter.setIncludeQueryString(true) ;
  reg.setFilter(filter) ;
  reg.addUrlPatterns("/api/*") ;
  return reg ;
}

這還不夠,還需要配合下面日志級別配置:

logging:
  level:
    '[org.springframework.web.filter]': debug

接下來,訪問/api/query/book?keyword=java接口

圖片圖片

成功記錄了請求的相關(guān)數(shù)據(jù)。

2.2 HandlerInterceptor記錄日志

HandlerInterceptor用于攔截請求處理也是非常的實用。它允許開發(fā)者在請求到達控制器之前、之后或發(fā)生異常時執(zhí)行特定邏輯,從而實現(xiàn)對請求處理的全面監(jiān)控和日志記錄。

定義攔截器

@Component
public class LoggingRequestInterceptor implements HandlerInterceptor {


  private static final Logger logger = LoggerFactory.getLogger(LoggingRequestInterceptor.class);


  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
    long startTime = Instant.now().toEpochMilli();
    String time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) ;
    logger.info("Request URL::" + request.getRequestURL().toString() + ":: StartTime=" + time);
    request.setAttribute("startTime", startTime);
    return true;
  }
  @Override
  public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    long startTime = (Long) request.getAttribute("startTime");
    logger.info("Request URL::" + request.getRequestURL().toString() + ":: TimeTaken="
        + (Instant.now().toEpochMilli() - startTime));
  }
}

注冊攔截器

@Component
public class InterceptorConfig implements WebMvcConfigurer {
  
  private final LoggingRequestInterceptor loggingRequestInterceptor ;
  public InterceptorConfig(LoggingRequestInterceptor loggingRequestInterceptor) {
    this.loggingRequestInterceptor = loggingRequestInterceptor;
  }
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(loggingRequestInterceptor).addPathPatterns("/api/**") ;
  }
}

請求API接口

圖片

2.3 使用AOP技術(shù)

通過AOP記錄日志是一種強大的技術(shù),它允許開發(fā)者在不修改業(yè)務(wù)邏輯代碼的情況下,將日志記錄等橫切關(guān)注點與業(yè)務(wù)邏輯分離。AOP能夠自動攔截方法調(diào)用,并記錄方法執(zhí)行前后的信息,為系統(tǒng)提供全面的日志支持。

自定義注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface Log {


  /**模塊*/
  String module() default "" ;
  /**具體操作說明*/
  String desc() default "" ;
}

定義切面

@Component
@Aspect
public class LogAspect {


  @Pointcut("@annotation(log)")
  private void recordLog(Log log) {}
  
  @Around("recordLog(log)")
  public Object logAround(ProceedingJoinPoint pjp, Log log) throws Throwable {
    String module = log.module();
    String desc = log.desc();
    long uid = System.nanoTime() ;
    String threadName = Thread.currentThread().getName();
    System.err.printf("%s - 【%d】 模塊: %s, 操作: %s, 請求參數(shù): %s%n", threadName, uid, module, desc, Arrays.toString(pjp.getArgs())) ;
    Object ret = pjp.proceed() ;
    System.err.printf("%s - 【%d】 返回值: %s%n", threadName, uid, ret) ;
    return ret ;
  }
}

修改Controller接口

@Log(module = "綜合查詢", desc = "查詢商品信息")
@GetMapping("/query/{category}")
public ResponseEntity<Object> query(@PathVariable String category, @RequestParam String keyword)

請求API接口

圖片圖片

2.4 基于Servlet請求事件機制

默認情況下,當一個請求到達后,Spring MVC底層會發(fā)布一個事件ServletRequestHandledEvent,我們只需要監(jiān)聽該事件也是可以獲取到詳細的請求/響應(yīng)信息。

自定義事件監(jiān)聽器

@Component
public class RequestEventListener implements ApplicationListener<ServletRequestHandledEvent> {


  private static final Logger logger = LoggerFactory.getLogger(RequestEventListener.class) ;
  @Override
  public void onApplicationEvent(ServletRequestHandledEvent event) {
    logger.info("請求信息: {}", event) ;
  } 
}

請求API接口

圖片圖片

你可以通過ServletRequestHandledEvent事件對象獲取具體的明細信息

圖片圖片

2.5 使用第三方Logbook組件

Logbook是一個可擴展的Java庫,能夠為不同的客戶端和服務(wù)器端技術(shù)實現(xiàn)完整的請求和響應(yīng)日志記錄。它允許開發(fā)者記錄應(yīng)用程序接收或發(fā)送的任何HTTP流量。

引入依賴

<dependency>
  <groupId>org.zalando</groupId>
  <artifactId>logbook-spring-boot-starter</artifactId>
  <version>3.10.0</version>
</dependency>

配置文件

logging:
  level:
    '[org.zalando.logbook.Logbook]': TRACE

請求API接口

圖片圖片

2.6 自定義HandlerMethod

如果你僅僅是針對Controller接口進行日志記錄處理,那么強烈推薦此種方式。

2.7 三方API接口調(diào)用

通常我們會使用RestTemplate/RestClient進行第三方接口的調(diào)用;如果要記錄此種情況的日志信息,那么我們需要自定義ClientHttpRequestInterceptor攔截器。

public class LoggingClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {


  private final Logger log = LoggerFactory.getLogger(this.getClass());
  @Override
  public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
      throws IOException {
    logRequest(request, body) ;
    ClientHttpResponse response = execution.execute(request, body);
    logResponse(response);
    return response;
  }
  private void logRequest(HttpRequest request, byte[] body) throws IOException {
    if (log.isDebugEnabled()) {
      log.debug("===========================request begin================================================");
      log.debug("URI         : {}", request.getURI());
      log.debug("Method      : {}", request.getMethod());
      log.debug("Headers     : {}", request.getHeaders());
      log.debug("Request body: {}", new String(body, "UTF-8"));
      log.debug("==========================request end================================================");
    }
  }
  private void logResponse(ClientHttpResponse response) throws IOException {
    if (log.isDebugEnabled()) {
      log.debug("============================response begin==========================================");
      log.debug("Status code  : {}", response.getStatusCode());
      log.debug("Status text  : {}", response.getStatusText());
      log.debug("Headers      : {}", response.getHeaders());
      log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
      log.debug("=======================response end=================================================");
    }
  }
}

RestTemplate配置攔截器

@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
  RestTemplate restTemplate = builder
    .additionalInterceptors(
      new LoggingClientHttpRequestInterceptor())
  .build() ;
  return restTemplate ;
}

日志打印情況如下:

圖片圖片

如果使用的RestClient,那么可以同一個攔截器,配置如下:

@Bean
RestClient restClient() {
  return RestClient
      .builder()
      .requestInterceptor(
        new LoggingClientHttpRequestInterceptor())
      .build() ;
}
責(zé)任編輯:武曉燕 來源: Spring全家桶實戰(zhàn)案例源碼
相關(guān)推薦

2024-05-30 08:51:28

Spring數(shù)據(jù)分布式

2024-08-30 11:11:01

2017-06-26 10:35:58

前端JavaScript繼承方式

2024-10-12 08:18:21

Spring接口組件

2025-01-21 08:00:00

限流微服務(wù)算法

2009-02-11 09:46:00

ASON網(wǎng)絡(luò)演進

2025-01-08 09:35:55

Spring性能監(jiān)控

2023-03-08 16:54:28

MySQL數(shù)據(jù)庫

2024-12-18 16:19:51

2017-06-14 16:44:15

JavaScript原型模式對象

2018-06-10 16:31:12

2025-02-27 00:00:30

SpringJava方式

2019-05-16 13:00:18

異步編程JavaScript回調(diào)函數(shù)

2024-01-04 12:48:00

Spring

2022-12-06 10:39:43

Spring事務(wù)失效

2022-03-18 14:33:22

限流算法微服務(wù)

2022-07-01 08:00:44

異步編程FutureTask

2021-01-08 10:52:22

物聯(lián)網(wǎng)萬物互聯(lián)IoT,Interne

2022-03-23 12:55:50

農(nóng)業(yè)物聯(lián)網(wǎng)

2023-05-10 13:58:13

服務(wù)限流系統(tǒng)
點贊
收藏

51CTO技術(shù)棧公眾號