我正在编写一个过滤器来记录所有请求及其响应
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)
}
}
}
}
我还需要记录请求和响应正文。这些隐藏在迭代器/枚举器中,是否有一种更简单的方法可以访问主体而无需详细了解迭代器的工作原理?否则如何将请求和响应正文转换为字符串?