How to log all requests for an http4s client
Asked Answered
W

2

10

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)

Wastepaper answered 4/4, 2018 at 11:7 Comment(0)
S
11

I got it working:

  • maven
  • https: 0.20.0-M6
  • slf4j-api: 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

Singsong answered 13/3, 2019 at 9:9 Comment(0)
M
1

You can use the provided middleware in http4s version 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. Example:

clientWithRequestLogging.fetch(Request(method = GET, uri = aUri))
Marginalia answered 16/8, 2022 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.