withLog
Description
Allows running an inner route using an alternative LoggingAdapter
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
- Scala
-
source
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!" }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.withLog; final LoggingAdapter special = Logging.getLogger(system(), "SpecialRoutes"); final Route sample = path( "sample", () -> extractLog( log -> { final String msg = "Logging using " + log + "!"; log.debug(msg); return complete(msg); })); final Route route = Directives.concat(pathPrefix("special", () -> withLog(special, () -> sample)), sample); // tests: testRoute(route) .run(HttpRequest.GET("/sample")) .assertEntity("Logging using " + system().log() + "!"); testRoute(route) .run(HttpRequest.GET("/special/sample")) .assertEntity("Logging using " + special + "!");
1.0.1