簡體   English   中英

如何記錄對 http4s 客戶端的所有請求

[英]How to log all requests for an http4s client

我想記錄我的應用程序發出的所有請求。 該應用程序會像這樣進行多次調用:

val client: Client = org.http4s.client.blaze.SimpleHttp1Client(...)
client.fetch(Request(method = GET, uri = aUri))

有沒有辦法讓客戶端將所有請求都記錄到文件中?

(使用 v0.12.4)

我搞定了:

  • 行家
  • https:0.20.0-M6
  • slf4j-api:1.7.26
  • slf4j-log4j12:1.7.26

根據這個問題,您必須將代碼修改為:

import org.http4s.client.middleware.Logger

val client: Client = org.http4s.client.blaze.SimpleHttp1Client(...)
Logger(logBody = true, logHeaders = true)(client)
    .fetch(Request(method = GET, uri = aUri))

所以你必須使用Logger包裝客戶端

您可以在 http4s 版本 0.23.14 中使用提供的中間件:

import org.http4s.client.Client
import org.http4s.client.middleware.RequestLogger
import cats.effect.IO

def client: Client[IO] = ???
val clientWithRequestLogging: Client[IO] = RequestLogger(logHeaders = true, logBody = true)(client)

然后可以以通常使用Client[F]的方式使用clientWithRequestLogging 例子:

clientWithRequestLogging.fetch(Request(method = GET, uri = aUri))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM