Class Slf4jLogHandler
java.lang.Object
io.leandev.appfuse.http.logging.Slf4jLogHandler
- All Implemented Interfaces:
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);
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescription建立 Slf4j 日誌處理器(預設配置)Slf4jLogHandler(Slf4jLogHandler.LogLevel requestLogLevel) 建立 Slf4j 日誌處理器Slf4jLogHandler(Slf4jLogHandler.LogLevel requestLogLevel, boolean includeBody) 建立 Slf4j 日誌處理器 -
Method Summary
Modifier and TypeMethodDescriptionvoidhandleRequest(HttpRequestLog requestLog) 處理 HTTP 請求日誌voidhandleResponse(HttpResponseLog responseLog) 處理 HTTP 回應日誌
-
Constructor Details
-
Slf4jLogHandler
public Slf4jLogHandler()建立 Slf4j 日誌處理器(預設配置)
預設使用 INFO 等級,不包含 Body 內容
-
Slf4jLogHandler
建立 Slf4j 日誌處理器- Parameters:
requestLogLevel- 請求日誌等級
-
Slf4jLogHandler
建立 Slf4j 日誌處理器- Parameters:
requestLogLevel- 請求日誌等級includeBody- 是否包含請求/回應 Body
-
-
Method Details
-
handleRequest
Description copied from interface:HttpLogHandler處理 HTTP 請求日誌
注意:此方法不應該拋出異常,否則會影響 HTTP 請求的執行
- Specified by:
handleRequestin interfaceHttpLogHandler- Parameters:
requestLog- 請求日誌
-
handleResponse
Description copied from interface:HttpLogHandler處理 HTTP 回應日誌
注意:此方法不應該拋出異常,否則會影響 HTTP 請求的執行
- Specified by:
handleResponsein interfaceHttpLogHandler- Parameters:
responseLog- 回應日誌
-