Last active
December 11, 2015 00:09
-
-
Save francisdb/4514950 to your computer and use it in GitHub Desktop.
Logging WS requests in playframework scala
This file contains 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
// I'm looking for a better solution for the time logging (preferable in a transparent way) | |
private def logTime(message: String, promise: Future[Response]) = { | |
// not 100% ok, request is already running | |
// might even not be started yet | |
val now = System.currentTimeMillis | |
promise.onRedeem { response => | |
val time = System.currentTimeMillis - now | |
logger.info(message + " => " + response.status + " " + time + "ms " + response.body.length + "b") | |
} | |
promise | |
} | |
def user(slug: String): Future[Option[User]] = { | |
val url = ... | |
logTime(url, | |
WS.url(url).get() | |
).map { | |
...handle response | |
} | |
} | |
Being more fresh at the morning... I'm seeing my error! Pimping won't work because, it won't let you use the rest lazily (or by-name). So that's not a solution.
And, it'll be hard to get something quite integrated because we switch from a type to another in this call (from WSRequestHolder and Future) and since the get
call is already creating the future for us it's even harder because we need to map it.
I thing the Play API is too restrictive at this point to allow a simpler solution :-/
I'll have to fork the play WS class then. Would have been nice if the async httpcliend had a global hook
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now I also want to get rid of the url being used twice and the logging to contain the get/post/delete...