Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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