Class Slf4jLogHandler

java.lang.Object
io.leandev.appfuse.http.logging.Slf4jLogHandler
All Implemented Interfaces:
HttpLogHandler

public class Slf4jLogHandler extends Object implements HttpLogHandler

Slf4j 日誌處理器

將 HTTP 請求與回應日誌輸出到 Slf4j Logger,是預設的日誌處理器實作。

日誌等級

  • INFO:成功的請求與回應(狀態碼 < 400)
  • WARN:客戶端錯誤(狀態碼 4xx)
  • ERROR:伺服器錯誤(狀態碼 5xx)或異常

日誌格式

[請求] POST https://api.example.com/users | RequestId: abc123 | Authenticated: true
[回應] 201 Created | RequestId: abc123 | Duration: 350ms | Retries: 0
[錯誤] 500 Internal Server Error | RequestId: abc123 | Error: Connection timeout

使用範例

// 使用預設配置
HttpLogHandler handler = new Slf4jLogHandler();

// 自訂日誌等級
HttpLogHandler handler = new Slf4jLogHandler(LogLevel.DEBUG);

// 包含 Body 內容
HttpLogHandler handler = new Slf4jLogHandler(LogLevel.DEBUG, true);
  • Constructor Details

    • Slf4jLogHandler

      public Slf4jLogHandler()

      建立 Slf4j 日誌處理器(預設配置)

      預設使用 INFO 等級,不包含 Body 內容

    • Slf4jLogHandler

      public Slf4jLogHandler(Slf4jLogHandler.LogLevel requestLogLevel)
      建立 Slf4j 日誌處理器
      Parameters:
      requestLogLevel - 請求日誌等級
    • Slf4jLogHandler

      public Slf4jLogHandler(Slf4jLogHandler.LogLevel requestLogLevel, boolean includeBody)
      建立 Slf4j 日誌處理器
      Parameters:
      requestLogLevel - 請求日誌等級
      includeBody - 是否包含請求/回應 Body
  • Method Details

    • handleRequest

      public void handleRequest(HttpRequestLog requestLog)
      Description copied from interface: HttpLogHandler

      處理 HTTP 請求日誌

      注意:此方法不應該拋出異常,否則會影響 HTTP 請求的執行

      Specified by:
      handleRequest in interface HttpLogHandler
      Parameters:
      requestLog - 請求日誌
    • handleResponse

      public void handleResponse(HttpResponseLog responseLog)
      Description copied from interface: HttpLogHandler

      處理 HTTP 回應日誌

      注意:此方法不應該拋出異常,否則會影響 HTTP 請求的執行

      Specified by:
      handleResponse in interface HttpLogHandler
      Parameters:
      responseLog - 回應日誌