withLog

Signature

def withLog(log: LoggingAdapter): Directive0

Description

Allows running an inner route using an alternative LoggingAdapter in place of the default one.

The logging adapter can be extracted in an inner route using extractLog directly, or used by directives which internally extract the materializer without surfacing this fact in the API.

Example

val special = Logging(system, "SpecialRoutes")

def sample() =
  path("sample") {
    extractLog { implicit log =>
      complete {
        val msg = s"Logging using $log!"
        log.debug(msg)
        msg
      }
    }
  }

val route =
  pathPrefix("special") {
    withLog(special) {
      sample() // `special` logging adapter will be used
    }
  } ~ sample() // default logging adapter will be used

// tests:
Get("/sample") ~> route ~> check {
  responseAs[String] shouldEqual s"Logging using ${system.log}!"
}
Get("/special/sample") ~> route ~> check {
  responseAs[String] shouldEqual s"Logging using $special!"
}
The source code for this page can be found here.