Created
May 28, 2025 10:59
-
-
Save lenguyenthanh/bc1fa4377e0d67d4a356a4a909e3c3b8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object HttpRoutesLogger { | |
import cats.data.{Kleisli, OptionT} | |
import cats.syntax.all.* | |
import org.http4s.internal.Logger as Http4sLogger | |
import org.http4s.{HttpRoutes, Request, Response} | |
import org.typelevel.log4cats.Logger | |
def isResponseError[F[_]](res: Response[F]): Boolean = | |
!res.status.isSuccess | |
private def logError[F[_]: {Concurrent, Logger}](req: Request[F], res: Response[F]): F[Unit] = | |
Http4sLogger.logMessage(req)(true, true)(Logger[F].warn) *> | |
Http4sLogger.logMessage(res)(true, true)(Logger[F].warn) | |
def apply[F[_]: {Concurrent, Logger}]: HttpRoutes[F] => HttpRoutes[F] = http => | |
Kleisli: req => | |
http(req).flatTap: res => | |
OptionT.liftF(logError(req, res)).whenA(isResponseError(res)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment