Last active
December 19, 2015 17:29
-
-
Save DrewEaster/5991632 to your computer and use it in GitHub Desktop.
Play! filter for performing access logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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