add rpc logger provider

This commit is contained in:
dongwei 2019-01-19 15:38:53 +08:00
parent 5fd6097604
commit 3e99169d00
2 changed files with 25 additions and 11 deletions

View File

@ -21,11 +21,6 @@ class Client
*/ */
protected $id; protected $id;
/**
* logger
* @var Logger
*/
protected $logger;
/** /**
* @var \GuzzleHttp\Client * @var \GuzzleHttp\Client
*/ */
@ -45,12 +40,7 @@ class Client
'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class, 'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class,
]; ];
$this->config = array_merge($default, $config); $this->config = array_merge($default, $config);
$stream = new StreamHandler(app()->storagePath() . $this->config['log_path']);
$stream->setFormatter(new $this->config['log_formatter']());
$logger = new Logger('RPC.LOGGER');
$logger->pushHandler($stream);
$this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id"; $this->id = app('request')->header('X-Request-Id') ?: "no-x-request-id";
$this->logger = $logger;
} }
/** /**
@ -124,11 +114,11 @@ class Client
try { try {
$body = \GuzzleHttp\json_decode($resp->getBody(), true); $body = \GuzzleHttp\json_decode($resp->getBody(), true);
app('log')->info('client call return body', $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) {

View File

@ -9,8 +9,32 @@
namespace JsonRpc\Providers; namespace JsonRpc\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use JsonRpc\Exception\RpcServerException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
class LoggerServiceProvider extends ServiceProvider class LoggerServiceProvider extends ServiceProvider
{ {
public function register()
{
$this->app->configure('rpc');
$config = config('rpc');
if (!is_array($config)) {
throw new RpcServerException("Application's Rpc Config Undefind", 500);
}
$this->app->singleton("rpc.logger", function () use ($config) {
$default = [
'app' => '***',
'log_path' => "/logs/rpc_monitor_" . date("Ymd") . ".log",
'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class,
];
$config = array_merge($default, $config);
$stream = new StreamHandler($this->app->storagePath() . $config['log_path']);
$stream->setFormatter(new $config['log_formatter']());
$logger = new Logger('RPC.LOGGER');
return $logger->pushHandler($stream);
});
}
} }