2016-09-21 53 views

回答

1

您可以在文檔看看LogResult Directive

這裏您可以找到的文檔中所示代碼相同的文檔片斷:

// different possibilities of using logResponse 

// The first alternatives use an implicitly available LoggingContext for logging 
// marks with "get-user", log with debug level, HttpResponse.toString 
DebuggingDirectives.logResult("get-user") 

// marks with "get-user", log with info level, HttpResponse.toString 
DebuggingDirectives.logResult(("get-user", Logging.InfoLevel)) 

// logs just the response status at debug level 
def responseStatus(res: RouteResult): String = res match { 
    case RouteResult.Complete(x)   => x.status.toString 
    case RouteResult.Rejected(rejections) => "Rejected: " + rejections.mkString(", ") 
} 
DebuggingDirectives.logResult(responseStatus _) 

// logs just the response status at info level 
def responseStatusAsInfo(res: RouteResult): LogEntry = LogEntry(responseStatus(res), Logging.InfoLevel) 
DebuggingDirectives.logResult(responseStatusAsInfo _) 

// This one doesn't use the implicit LoggingContext but uses `println` for logging 
def printResponseStatus(res: RouteResult): Unit = println(responseStatus(res)) 
val logResultPrintln = DebuggingDirectives.logResult(LoggingMagnet(_ => printResponseStatus)) 

// tests: 
Get("/") ~> logResultPrintln(complete("logged")) ~> check { 
    responseAs[String] shouldEqual "logged" 
} 
相關問題