简体   繁体   English

如何记录对 http4s 客户端的所有请求

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

I want to log all the requests my application makes.我想记录我的应用程序发出的所有请求。 The application makes several call like this:该应用程序会像这样进行多次调用:

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

Is there a way of getting the client to log to a file all the requests?有没有办法让客户端将所有请求都记录到文件中?

(Using v0.12.4) (使用 v0.12.4)

I got it working: 我搞定了:

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

Based on the question, you have to modify your code to this: 根据这个问题,您必须将代码修改为:

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))

So you have to wrap the client with a Logger 所以你必须使用Logger包装客户端

You can use the provided middleware in http4s version 0.23.14:您可以在 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)

clientWithRequestLogging can then be used in the usual way Client[F] is used.然后可以以通常使用Client[F]的方式使用clientWithRequestLogging Example:例子:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM