add rpc logger

This commit is contained in:
dongwei 2019-01-19 16:53:19 +08:00
parent 420725e262
commit c388c4d845
2 changed files with 6 additions and 8 deletions

View File

@ -34,12 +34,7 @@ class Client
public function __construct($config) public function __construct($config)
{ {
$default = [ $this->config = $config;
'app' => '***',
'log_path' => "/logs/rpc_monitor_" . date("Ymd") . ".log",
'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class,
];
$this->config = array_merge($default, $config);
$this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id"; $this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id";
} }
@ -104,6 +99,7 @@ class Client
'client_host' => gethostname(), 'client_host' => gethostname(),
'client_addr' => $_SERVER['SERVER_ADDR'], 'client_addr' => $_SERVER['SERVER_ADDR'],
]; ];
app('rpc.logger')->info("client_request",array_merge($this->server_config, $payload));
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [ $resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [
'headers' => $headers, 'headers' => $headers,
'json' => $payload, 'json' => $payload,
@ -114,14 +110,15 @@ class Client
try { try {
$body = \GuzzleHttp\json_decode($resp->getBody(), true); $body = \GuzzleHttp\json_decode($resp->getBody(), true);
app('rpc.logger')->info("client_response",$body);
if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) { if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) {
$message = is_array($body['error']['message']) ? json_encode($body['error']['message']) : $body['error']['message']; $message = is_array($body['error']['message']) ? json_encode($body['error']['message']) : $body['error']['message'];
throw new RpcServerException($message, $body['error']['code']); throw new RpcServerException($message, $body['error']['code']);
} }
app('rpc.logger')->info('rpc call log new return');
return $body['result']; return $body['result'];
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {
app('rpc.logger')->error('client_decode_error',array_merge($this->server_config, $payload));
throw new RpcServerException('json decode error', -32700); throw new RpcServerException('json decode error', -32700);
} }
} }

View File

@ -63,8 +63,9 @@ class JsonRpcServer
return $this->error(self::Rpc_Error_Invalid_Params); return $this->error(self::Rpc_Error_Invalid_Params);
} }
app('log')->info('rpc ser', [$method, $params, $id, $class, $this->request->header('client_app')]); app('rpc.logger')->info('server', [$id, $class,$method, $params, $this->request->header('client_app'), $this->request->header('client_addr'), $this->request->header('client_host')]);
$ret = call_user_func_array([(new $class($id, $this->request)), $function], $params); $ret = call_user_func_array([(new $class($id, $this->request)), $function], $params);
app('rpc.logger')->info('server_result', [$id, $ret]);
return $ret; return $ret;
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {