Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.53 KB | None | 0 0
  1.   def requestResponseLog(route: Route) = {
  2.     def requestResponseLogging(loggingAdapter: LoggingAdapter)(req: HttpRequest)(res: Any): Unit = {
  3.       val timeout = 9000.millis
  4.       val bodyAsBytes: Future[ByteString] = req.entity.toStrict(timeout).map(_.data)
  5.       val bodyAsString: Future[String] = bodyAsBytes.map(_.utf8String)
  6.       bodyAsString.onComplete {
  7.         case Success(body) =>
  8.           logger.debug(s"$req\nRequest body: $body\n")
  9.         case Failure(t) =>
  10.           logger.warn(s"error, ${t.getMessage} Failed to get the body for: $req\n")
  11.       }
  12.       res match {
  13.         case Complete(resp) =>
  14.           logger.debug(s"response headers555=${resp.headers}\nresponse status=${resp.status}")
  15.           val bodyAsBytes: Future[ByteString] = resp.entity.toStrict(timeout).map(_.data)
  16.           val bodyAsString: Future[String] = bodyAsBytes.map(_.utf8String)
  17.           bodyAsString.onComplete {
  18.             case Success(value) => {
  19.               logger.debug(s"Response body: ${value}\n")
  20.             }
  21.             case Failure(exception) => {
  22.               logger.warn(s"Failed to get the body for: $resp\n")
  23.             }
  24.           }
  25.         case anythingElse => {
  26.           logger.debug(s"$anythingElse")
  27.         }
  28.       }
  29.     }
  30.     DebuggingDirectives.logRequestResult(LoggingMagnet(requestResponseLogging))(route)
  31.   }
  32.  
  33.   val bindingFuture = Http().bindAndHandle(requestResponseLog(route), Conf.Server.host, Conf.Server.port)
  34.   logger.info(s"Server online at http://${Conf.Server.host}:${Conf.Server.port}/")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement