Skip to content

Commit

Permalink
Update GrpcLogger.php
Browse files Browse the repository at this point in the history
  • Loading branch information
reatang committed Jul 27, 2023
1 parent 71a2cf3 commit 3a16ba1
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/Middlewares/GrpcLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

0 comments on commit 3a16ba1

Please sign in to comment.