logRequest
Signature
def logRequest(marker: String): Directive0
def logRequest(marker: String, level: LogLevel): Directive0
def logRequest(show: HttpRequest => String): Directive0
def logRequest(show: HttpRequest => LogEntry): Directive0
def logRequest(magnet: LoggingMagnet[HttpRequest => Unit]): Directive0
The signature shown is simplified, the real signature uses magnets. [1]
[1] See The Magnet Pattern for an explanation of magnet-based overloading.
Description
Logs the request using the supplied LoggingMagnet[HttpRequest => Unit]
using the LoggingAdapter
LoggingAdapter
of the RequestContext
RequestContext
. The LoggingMagnet
is a wrapped function HttpRequest => Unit
that can be implicitly created from the different constructors shown above. These constructors build a LoggingMagnet
from these components: Logs the request. The directive is available with the following parameters:
It is also possible to use any other function HttpRequest => Unit
for logging by wrapping it with LoggingMagnet
. See the examples for ways to use the logRequest
directive.
Use logResult
for logging the response, or logRequestResult
for logging both.
To change the logger, wrap this directive by withLog.
Example
- Scala
-
source
// different possibilities of using logRequest // The first alternatives use an implicitly available LoggingContext for logging // marks with "get-user", log with debug level, HttpRequest.toString DebuggingDirectives.logRequest("get-user") // marks with "get-user", log with info level, HttpRequest.toString DebuggingDirectives.logRequest(("get-user", Logging.InfoLevel)) // logs just the request method at debug level def requestMethod(req: HttpRequest): String = req.method.name DebuggingDirectives.logRequest(requestMethod _) // logs just the request method at info level def requestMethodAsInfo(req: HttpRequest): LogEntry = LogEntry(req.method.name, Logging.InfoLevel) DebuggingDirectives.logRequest(requestMethodAsInfo _) // This one doesn't use the implicit LoggingContext but uses `println` for logging def printRequestMethod(req: HttpRequest): Unit = println(req.method.name) val logRequestPrintln = DebuggingDirectives.logRequest(LoggingMagnet(_ => printRequestMethod)) // tests: Get("/") ~> logRequestPrintln(complete("logged")) ~> check { responseAs[String] shouldEqual "logged" }
- Java
-
source
import static org.apache.pekko.http.javadsl.server.Directives.complete; import static org.apache.pekko.http.javadsl.server.Directives.get; import static org.apache.pekko.http.javadsl.server.Directives.logRequest; // logs request with "get-user" final Route routeBasicLogRequest = get(() -> logRequest("get-user", () -> complete("logged"))); // logs request with "get-user" as Info final Route routeBasicLogRequestAsInfo = get(() -> logRequest("get-user", InfoLevel(), () -> complete("logged"))); // logs just the request method at info level Function<HttpRequest, LogEntry> requestMethodAsInfo = (request) -> LogEntry.create(request.method().name(), InfoLevel()); final Route routeUsingFunction = get(() -> logRequest(requestMethodAsInfo, () -> complete("logged"))); // tests: testRoute(routeBasicLogRequest).run(HttpRequest.GET("/")).assertEntity("logged");