Skip to content

Instantly share code, notes, and snippets.

@DrewEaster
Last active December 19, 2015 17:29
Show Gist options
  • Save DrewEaster/5991632 to your computer and use it in GitHub Desktop.
Save DrewEaster/5991632 to your computer and use it in GitHub Desktop.
Play! filter for performing access logging
object AccessLogFilter extends EssentialFilter {
val dateTimeFormat = ISODateTimeFormat.ordinalDateTimeNoMillis()
def apply(next: EssentialAction) = new EssentialAction {
def apply(rh: RequestHeader) = {
val startTime = System.currentTimeMillis()
def logTime(result: PlainResult): Result = {
val event = Json.obj(
"uri" -> rh.uri,
"timestamp" -> dateTimeFormat.print(new DateTime),
"execution_time" -> (System.currentTimeMillis() - startTime),
"status" -> result.header.status,
"tags" -> Json.toJson(rh.tags.map(entry => entry._1.toLowerCase -> entry._2))
)
Logger.info(Json.stringify(event))
result
}
next(rh).map {
case plain: PlainResult => logTime(plain)
case async: AsyncResult => async.transform(logTime)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment