update request log middleware
This commit is contained in:
@@ -53,8 +53,8 @@ class RequestLogMiddleware implements MiddlewareInterface
|
|||||||
|
|
||||||
$response_code = self::extractResponseCode($response);
|
$response_code = self::extractResponseCode($response);
|
||||||
|
|
||||||
// 异步协程写库,不阻塞响应返回
|
// defer 写库:在当前协程退出时执行,响应已返回客户端,同时保证协程内 DB 连接可用
|
||||||
Coroutine::create(static function () use (
|
Coroutine::defer(static function () use (
|
||||||
$user_id, $method, $path, $status_code, $ip,
|
$user_id, $method, $path, $status_code, $ip,
|
||||||
$user_agent, $request_body, $response_code, $duration_ms
|
$user_agent, $request_body, $response_code, $duration_ms
|
||||||
): void {
|
): void {
|
||||||
@@ -110,7 +110,12 @@ class RequestLogMiddleware implements MiddlewareInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$body = (string) $response->getBody();
|
$stream = $response->getBody();
|
||||||
|
$body = (string) $stream;
|
||||||
|
// 回退流指针,避免后续读取得到空内容
|
||||||
|
if ($stream->isSeekable()) {
|
||||||
|
$stream->rewind();
|
||||||
|
}
|
||||||
$data = json_decode($body, true);
|
$data = json_decode($body, true);
|
||||||
if (is_array($data) && isset($data['code'])) {
|
if (is_array($data) && isset($data['code'])) {
|
||||||
return (int) $data['code'];
|
return (int) $data['code'];
|
||||||
|
|||||||
@@ -11,5 +11,6 @@ declare(strict_types=1);
|
|||||||
*/
|
*/
|
||||||
return [
|
return [
|
||||||
'http' => [
|
'http' => [
|
||||||
|
\App\Middleware\RequestLogMiddleware::class,
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user