client headers in log

This commit is contained in:
dongwei 2019-01-11 17:26:01 +08:00
parent 1bcb8a010a
commit d67bb4aefe

View File

@ -9,16 +9,30 @@ use Monolog\Logger;
class Client class Client
{ {
/**
* all configuration information
* @var array
*/
protected $config; protected $config;
/**
* request id
* @var string
*/
protected $id; protected $id;
/**
* logger
* @var Logger
*/
protected $logger; protected $logger;
/** /**
* @var \GuzzleHttp\Client * @var \GuzzleHttp\Client
*/ */
protected $http; protected $http;
protected $server_config;
public function __construct($config) public function __construct($config)
{ {
$default = [ $default = [
@ -35,16 +49,22 @@ class Client
$this->logger = $logger; $this->logger = $logger;
} }
/**
*
* @param $k
* @return $this
*/
public function endpoint($k) public function endpoint($k)
{ {
$config = $this->config[$k]; $this->server_config = $this->config[$k];
$default = [ $default = [
'app' => $k,
'timeout' => 3, 'timeout' => 3,
'allow_redirects' => false, 'allow_redirects' => false,
]; ];
$this->http = new \GuzzleHttp\Client(array_merge($default, $config)); $this->http = new \GuzzleHttp\Client(array_merge($default, $this->server_config));
return $this; return $this;
} }
@ -84,10 +104,12 @@ class Client
protected function post($payload) protected function post($payload)
{ {
try { try {
$headers = [
'client_app' => $this->config['app'],
'server_app' => $this->server_config['app']
];
$resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [ $resp = $this->http->request('POST', 'rpc/json-rpc-v2.json', [
'headers' => [ 'headers' => $headers,
'hwmc_app' => $this->config['app'],
],
'json' => $payload, 'json' => $payload,
]); ]);
} catch (ServerException $e) { } catch (ServerException $e) {
@ -99,7 +121,7 @@ class Client
if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) { if (isset($body['error']) && isset($body['error']['code']) && isset($body['error']['message'])) {
throw new RpcServerException($body['error']['message'], $body['error']['code']); throw new RpcServerException($body['error']['message'], $body['error']['code']);
} }
$this->logger->info('MONITOR',compact("payload", "body")); $this->logger->info('MONITOR',compact("payload", "body", "headers"));
return $body['result']; return $body['result'];
} catch (\InvalidArgumentException $e) { } catch (\InvalidArgumentException $e) {