我正在寻找在Play过滤器中记录响应体长的最佳方法。
有没有比下面更好的方法?我不清楚这种方法是否会在所有情况下记录请求。如果客户端提前关闭连接,是否可能无法完全使用枚举器?
class AccessLogFilter extends Filter with Results {
override def apply(next: RequestHeader => Future[Result])(request: RequestHeader): Future[Result] = {
result.onFailure {
case t: Throwable => // log access, error
}
result.map { value =>
var responseLength = 0L
val insturmentedBody = value.body.map { chunk =>
responseLength += chunk.length
chunk
}.onDoneEnumerating {
// log access with response length
}
Result(value.header, insturmentedBody, value.connection)
}
}
}