【问题标题】:logging response body with spray.io使用 spray.io 记录响应主体
【发布时间】:2017-01-16 22:14:10
【问题描述】:

下面的代码打印出来:

Testing started at 00:10 ...
Connected to the target VM, address: '127.0.0.1:58646', transport: 'socket'
00:10:10 INFO [lt-dispatcher-4] [a.e.s.Slf4jLogger        ] - Slf4jLogger started
vazzeg GET:200 OK
Disconnected from the target VM, address: '127.0.0.1:58646', transport: 'socket'

问题是我在任何地方都看不到 logged 这个词,它从服务器传输回客户端(在测试中)。

如何记录响应的内容?

代码:

class MainSpec extends FreeSpec with Matchers with ScalatestRouteTest with SpraySampleService {
  def actorRefFactory = system
  def requestMethodAndResponseStatusAsInfo(req: HttpRequest): Any => Option[LogEntry] = {
    case res: HttpResponse => Some(LogEntry(req.method + ":" + res.message.status, Logging.DebugLevel))
    case res =>  Some(LogEntry("vazzeg 2"+res))  // other kind of responses
  }
  def printRequestMethodAndResponseStatus(req: HttpRequest)(res: Any): Unit =
    println("vazzeg "+requestMethodAndResponseStatusAsInfo(req)(res).map(_.obj.toString).getOrElse(""))

  val logRequestResponsePrintln = DebuggingDirectives.logRequestResponse(LoggingMagnet(printRequestMethodAndResponseStatus))

  "The spraysample Route" - {
    "simple" in {
      Get("/") ~> logRequestResponsePrintln(complete("logged")) ~> check {
        val r=responseAs[String]
        r === "logged"

      }
    }
....
}

【问题讨论】:

    标签: scala spray


    【解决方案1】:

    这似乎有效:

      "The spraysample Route" - {
        "simple" in {
            Get("/") ~> mapHttpResponse({x=>{println("response: "+x.toString);x}})(complete("logged")) ~> check {
            val r=responseAs[String]
            r === "logged"
    
          }
        }
    

    打印:

    vazzeg: HttpResponse(200 OK,HttpEntity(text/plain; charset=UTF-8,logged),List(),HTTP/1.1)
    

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2018-10-22
      • 1970-01-01
      • 2018-07-26
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多