Log url of request on InternalServerError in http4s

908 views Asked by At

http4s returns InternalServerError if an unhandled error happened. Is it possible to log url of the request when InternalServerError happened? If yes, what is the best way to do it?

1

There are 1 answers

0
tentacle On BEST ANSWER

Isn't it doing this right out of the box?

Here's log from g8 template project from quick start guide https://http4s.org/v0.21/ with added route that throws runtime exception:

[ioapp-compute-1] INFO  o.h.s.m.Logger - HTTP/1.1 GET /bad_joke Headers(Accept: */*, Cache-Control: no-cache, Host: localhost:8089, Connection: Keep-Alive, User-Agent: Apache-HttpClient/4.5.12 (Java/11.0.8), Accept-Encoding: gzip,deflate) body="" 
[ioapp-compute-1] INFO  o.h.s.m.Logger - service raised an error: class java.lang.RuntimeException 
[ioapp-compute-1] ERROR o.h.s.service-errors - Error servicing request: GET /bad_joke from 127.0.0.1 
java.lang.RuntimeException: haha
        at pack.H4sRoutes$$anonfun$jokeRoutes$1.applyOrElse(H4sRoutes.scala:19)
        at pack.H4sRoutes$$anonfun$jokeRoutes$1.applyOrElse(H4sRoutes.scala:13)
        at $anonfun$combineK$1 @ org.http4s.syntax.KleisliResponseOps.$anonfun$orNotFound$1(KleisliSyntax.scala:38)
        at getOrElse @ org.http4s.syntax.KleisliResponseOps.$anonfun$orNotFound$1(KleisliSyntax.scala:38)
        at guaranteeCase$extension @ org.http4s.server.middleware.RequestLogger$.$anonfun$impl$9(RequestLogger.scala:97)
        at map @ org.http4s.server.middleware.RequestLogger$.$anonfun$impl$9(RequestLogger.scala:101)
        at flatMap @ org.http4s.server.middleware.RequestLogger$.$anonfun$impl$7(RequestLogger.scala:82)
        at flatMap @ org.http4s.server.middleware.ResponseLogger$.$anonfun$impl$7(ResponseLogger.scala:65)
        at guaranteeCase$extension @ org.http4s.server.middleware.ResponseLogger$.$anonfun$impl$7(ResponseLogger.scala:88)
        at main$ @ pack.Main$.main(Main.scala:5)