I'm writing a filter to log all requests and their responses
object LoggingFilter extends EssentialFilter {
def apply(next: EssentialAction) = new EssentialAction {
def apply(rh: RequestHeader) = {
val start = System.currentTimeMillis
def logTime(result: PlainResult): Result = result match {
case simple @ SimpleResult(header, content) =>
val time = System.currentTimeMillis - start
play.Logger.info(s"${rh.method} ${rh.uri} took ${time}ms and returned ${header.status}")
result
case _ => result
}
next(rh).map {
case plain: PlainResult => logTime(plain)
case async: AsyncResult => async.transform(logTime)
}
}
}
}
I need to also log the request and response bodies. These are buried inside Iterators/Enumerators, is there an easier way to access the bodies without having to go into details of how iteratees work? Otherwise how can I convert the request and response bodies as a string?
No, there is no easier way. But you can take a look on my Google HTML Compressor filter.
If you want to capture the SimpleResult response body use this:
Check mimetype for json: