Log request body with scala http filter

I am using this filter to keep a log of the request and response time:

import play.api.Logger
import play.api.mvc._
import scala.concurrent.Future
import play.api.libs.concurrent.Execution.Implicits.defaultContext

object LoggingFilter extends Filter {
  def apply(nextFilter: (RequestHeader) => Future[SimpleResult])
           (requestHeader: RequestHeader): Future[SimpleResult] = {
    val startTime = System.currentTimeMillis
    nextFilter(requestHeader).map { result =>
      val endTime = System.currentTimeMillis
      val requestTime = endTime - startTime
      Logger.info(s"${requestHeader.method} ${requestHeader.uri} " +
        s"took ${requestTime}ms and returned ${result.header.status}")
      result.withHeaders("Request-Time" -> requestTime.toString)
    }
  }
}

      

however I need to keep the request body in a log. I know it can be done with Scala action composition, but I can't figure out how to do it. any help?

+3


source to share


1 answer


If you want to access the request body, you need to override the apply(EssentialAction)

object method EssentialFilter

.

For reference:



0


source







All Articles