Created
February 4, 2018 06:53
-
-
Save taintech/c0201b255d3e8a7cecce3e327561258c to your computer and use it in GitHub Desktop.
Scala Utility methods for logging
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
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