From 71a2cf39dac0a56b83ff3c0896d97562e174aeb5 Mon Sep 17 00:00:00 2001 From: reatang Date: Thu, 27 Jul 2023 14:25:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BC=A0null=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Middlewares/GrpcLogger.php | 3 ++- src/Middlewares/GrpcRetry.php | 22 +++------------------- src/Utils/LoggerTrait.php | 9 ++++++--- 3 files changed, 11 insertions(+), 23 deletions(-) diff --git a/src/Middlewares/GrpcLogger.php b/src/Middlewares/GrpcLogger.php index 5e20c63..f91c29d 100644 --- a/src/Middlewares/GrpcLogger.php +++ b/src/Middlewares/GrpcLogger.php @@ -22,7 +22,8 @@ class GrpcLogger extends Interceptor */ protected $logger; - public function __construct(LoggerInterface $logger) { + public function __construct(LoggerInterface $logger) + { $this->setLogger($logger); } diff --git a/src/Middlewares/GrpcRetry.php b/src/Middlewares/GrpcRetry.php index 2116205..a880da8 100644 --- a/src/Middlewares/GrpcRetry.php +++ b/src/Middlewares/GrpcRetry.php @@ -7,6 +7,7 @@ use Grpc\UnaryCall; use Psr\Log\LoggerInterface; use Reatang\GrpcPHPAbstract\Call\ResponseCall; +use Reatang\GrpcPHPAbstract\Utils\LoggerTrait; use const Grpc\STATUS_UNAVAILABLE; use const Grpc\STATUS_ABORTED; @@ -17,6 +18,8 @@ */ class GrpcRetry extends Interceptor { + use LoggerTrait; + /** @var int 最大重试次数 */ protected $maxAttempts; @@ -25,13 +28,6 @@ class GrpcRetry extends Interceptor protected $retryableStatusCodes = []; - /** - * 日志记录 - * - * @var LoggerInterface - */ - protected $logger; - public function __construct( $maxAttempts = 3, $delay = 300, @@ -84,16 +80,4 @@ public function interceptUnaryUnary( return new ResponseCall(null, $status); } - - /** - * @param LoggerInterface $logger - * - * @return $this - */ - public function setLogger(LoggerInterface $logger) - { - $this->logger = $logger; - - return $this; - } } diff --git a/src/Utils/LoggerTrait.php b/src/Utils/LoggerTrait.php index d74f488..296b6e2 100644 --- a/src/Utils/LoggerTrait.php +++ b/src/Utils/LoggerTrait.php @@ -10,17 +10,20 @@ trait LoggerTrait protected $logger = null; /** - * @param LoggerInterface $logger + * @param LoggerInterface|null $logger * - * @return self + * @return $this */ - public function setLogger(LoggerInterface $logger) + public function setLogger(?LoggerInterface $logger) { $this->logger = $logger; return $this; } + /** + * @return LoggerInterface|null + */ protected function getLogger(): ?LoggerInterface { return $this->logger; From 3a16ba1b8fb39bba245261f862ff76347a37cfcf Mon Sep 17 00:00:00 2001 From: reatang Date: Thu, 27 Jul 2023 14:29:20 +0800 Subject: [PATCH 2/2] Update GrpcLogger.php --- src/Middlewares/GrpcLogger.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/Middlewares/GrpcLogger.php b/src/Middlewares/GrpcLogger.php index f91c29d..bb10eca 100644 --- a/src/Middlewares/GrpcLogger.php +++ b/src/Middlewares/GrpcLogger.php @@ -47,13 +47,12 @@ public function interceptUnaryUnary( private function responseLog($name, ?Message $argument, ?Message $response, $status, $startTime) { $t = (microtime(true) - $startTime) * 1000; - if ($this->logger) { - $this->writeLogInfo("[GRPC] CALL {$name} - Time: {$t}", [ - 'status' => "{$status->code}:$status->details", - 'request' => $this->decodeMessage($argument), - 'response' => $this->decodeMessage($response), - ]); - } + + $this->writeLogInfo("[GRPC] CALL {$name} - Time: {$t}", [ + 'status' => "{$status->code}:$status->details", + 'request' => $this->decodeMessage($argument), + 'response' => $this->decodeMessage($response), + ]); } private function decodeMessage(?Message $message) : ?array @@ -62,13 +61,13 @@ private function decodeMessage(?Message $message) : ?array return null; } + // 注意,php 的 serializeToJsonString 不支持 google.protobuf.Any 参数的自动解析为JSON,需手动转换 + // 如果 message 参数中存在Any类型,此处会报错 return json_decode($message->serializeToJsonString(), true); } protected function writeLogInfo($message, array $context = []) { - if ($this->logger) { - $this->logger->info($message, $context); - } + $this->logger->info($message, $context); } }