Akka-http logrequest 不记录请求正文
Akka-http logrequest not logging the request body
我正在使用 akka-http 并尝试使用 logrequest 在特定路径上记录请求:
path(Segment / "account") { id =>
logRequest("users/account", Logging.InfoLevel) {
post {
entity(as[Account]) { account => ???
complete(HttpResponse(StatusCodes.NoContent))
}
}
}
但是在我的日志中我看到类似
的内容
HttpRequest(HttpMethod(POST),https://localhost:9009/api/users/123/account,List(Host: localhost:9009, User-Agent: akka-http/10.0.6, Timeout-Access: <function1>),HttpEntity.Chunked(application/json),HttpProtocol(HTTP/1.1))
我要查找的是请求者发送的包含正文 (json) 的确切请求。
日志的"HttpEntity.Chunked(application/json)"
段是HttpEntity.Chunked#toString
. To get the entire request body, which is implemented as a stream, you need to call HttpEntity#toStrict
将Chunked
请求实体转换为Strict
请求实体的输出。您可以在自定义路由中进行此调用:
def logRequestEntity(route: Route, level: LogLevel)
(implicit m: Materializer, ex: ExecutionContext) = {
def requestEntityLoggingFunction(loggingAdapter: LoggingAdapter)(req: HttpRequest): Unit = {
val timeout = 900.millis
val bodyAsBytes: Future[ByteString] = req.entity.toStrict(timeout).map(_.data)
val bodyAsString: Future[String] = bodyAsBytes.map(_.utf8String)
bodyAsString.onComplete {
case Success(body) =>
val logMsg = s"$req\nRequest body: $body"
loggingAdapter.log(level, logMsg)
case Failure(t) =>
val logMsg = s"Failed to get the body for: $req"
loggingAdapter.error(t, logMsg)
}
}
DebuggingDirectives.logRequest(LoggingMagnet(requestEntityLoggingFunction(_)))(route)
}
要使用以上内容,请将您的路线传递给它:
val loggedRoute = logRequestEntity(route, Logging.InfoLevel)
我正在使用 akka-http 并尝试使用 logrequest 在特定路径上记录请求:
path(Segment / "account") { id =>
logRequest("users/account", Logging.InfoLevel) {
post {
entity(as[Account]) { account => ???
complete(HttpResponse(StatusCodes.NoContent))
}
}
}
但是在我的日志中我看到类似
的内容HttpRequest(HttpMethod(POST),https://localhost:9009/api/users/123/account,List(Host: localhost:9009, User-Agent: akka-http/10.0.6, Timeout-Access: <function1>),HttpEntity.Chunked(application/json),HttpProtocol(HTTP/1.1))
我要查找的是请求者发送的包含正文 (json) 的确切请求。
日志的"HttpEntity.Chunked(application/json)"
段是HttpEntity.Chunked#toString
. To get the entire request body, which is implemented as a stream, you need to call HttpEntity#toStrict
将Chunked
请求实体转换为Strict
请求实体的输出。您可以在自定义路由中进行此调用:
def logRequestEntity(route: Route, level: LogLevel)
(implicit m: Materializer, ex: ExecutionContext) = {
def requestEntityLoggingFunction(loggingAdapter: LoggingAdapter)(req: HttpRequest): Unit = {
val timeout = 900.millis
val bodyAsBytes: Future[ByteString] = req.entity.toStrict(timeout).map(_.data)
val bodyAsString: Future[String] = bodyAsBytes.map(_.utf8String)
bodyAsString.onComplete {
case Success(body) =>
val logMsg = s"$req\nRequest body: $body"
loggingAdapter.log(level, logMsg)
case Failure(t) =>
val logMsg = s"Failed to get the body for: $req"
loggingAdapter.error(t, logMsg)
}
}
DebuggingDirectives.logRequest(LoggingMagnet(requestEntityLoggingFunction(_)))(route)
}
要使用以上内容,请将您的路线传递给它:
val loggedRoute = logRequestEntity(route, Logging.InfoLevel)