Skip to content

Instantly share code, notes, and snippets.

@taintech
Created February 4, 2018 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taintech/c0201b255d3e8a7cecce3e327561258c to your computer and use it in GitHub Desktop.
Save taintech/c0201b255d3e8a7cecce3e327561258c to your computer and use it in GitHub Desktop.
Scala Utility methods for logging
trait Utility extends LazyLogging {
def log(future: Future[_])(
implicit executionContext: ExecutionContextExecutor): Unit = {
val start = System.currentTimeMillis()
future.onComplete {
case Success(res) =>
logger.info(
s"Result in ${System.currentTimeMillis() - start} ms is $res")
case Failure(e) =>
logger.error("Ne hud", e)
}
}
def logF[T](future: Future[T])(
implicit executionContext: ExecutionContextExecutor): Future[T] = {
val start = System.currentTimeMillis()
future.onComplete {
case Success(res) =>
logger.info(
s"Result in ${System.currentTimeMillis() - start} ms is $res")
case Failure(e) =>
logger.error("Ne hud", e)
}
future
}
def logEntity(future: Future[HttpResponse])(
implicit executionContext: ExecutionContextExecutor,
materializer: ActorMaterializer): Unit = {
val timeout = 2 seconds
val start = System.currentTimeMillis()
future.onComplete {
case Success(res) =>
logger.info(
s"Result in ${System.currentTimeMillis() - start} ms with entity is ${Await
.result(res.entity.toStrict(timeout).map { _.data }.map(_.utf8String), timeout)}")
case Failure(e) =>
logger.error("Ne hud", e)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment