Skip to content

Instantly share code, notes, and snippets.

@beschauer
Created August 22, 2013 14:12
Show Gist options
  • Save beschauer/6307712 to your computer and use it in GitHub Desktop.
Save beschauer/6307712 to your computer and use it in GitHub Desktop.
Logs all Request / Response Cycles. Log message contains HTTP-Method, Request Headers, Response Status and Response body.
import play.api.GlobalSettings
import play.api.Play.current
import play.api.mvc._
object Global extends WithFilters(LoggingFilter) {}
object LoggingFilter extends Filter {
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.iteratee.Iteratee
def apply(next: (RequestHeader) => Result)(rh: RequestHeader) = {
def logStatus(result: PlainResult): Result = {
result match {
case s: SimpleResult[_] => {
val bodyFuture = s.body run Iteratee.getChunks
bodyFuture map (bodyChunks => writeLog(rh, result, bodyChunks.mkString))
}
case _ => Logger.warn("LogginFilter missed response. Unhandled response type!")
}
result
}
def writeLog(rh: RequestHeader, result: PlainResult, resultBody: String) = {
val message = rh.method + " " + rh.path + rh.rawQueryString +
" returned with Status: " + result.header.status +
" Response Body: " + resultBody +
" Request-Headers: " + rh.headers.toSimpleMap.mkString
if (result.header.status >= 399) Logger.warn(message)
else Logger.debug(message)
}
next(rh) match {
case plain: PlainResult => logStatus(plain)
case async: AsyncResult => async.transform(logStatus)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment