【问题标题】:Log url of request on InternalServerError in http4s在 http4s 中记录 InternalServerError 请求的 url
【发布时间】:2020-11-23 11:18:52
【问题描述】:

如果发生未处理的错误,http4s 将返回 InternalServerError。 发生 InternalServerError 时是否可以记录请求的 url? 如果是,最好的方法是什么?

【问题讨论】:

    标签: scala http4s


    【解决方案1】:

    这不是开箱即用的吗?

    这是来自快速入门指南https://http4s.org/v0.21/ 的 g8 模板项目的日志 添加了引发运行时异常的路由:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2018-08-02
      相关资源
      最近更新 更多