From 3e99169d00f65c0bb9cea0e3b67ef5008c0bf55d Mon Sep 17 00:00:00 2001 From: dongwei <838456736@qq.com> Date: Sat, 19 Jan 2019 15:38:53 +0800 Subject: [PATCH] add rpc logger provider --- src/Client.php | 12 +----------- src/Providers/LoggerServiceProvider.php | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/Client.php b/src/Client.php index 46caaef..8d4fbc5 100644 --- a/src/Client.php +++ b/src/Client.php @@ -21,11 +21,6 @@ class Client */ protected $id; - /** - * logger - * @var Logger - */ - protected $logger; /** * @var \GuzzleHttp\Client */ @@ -45,12 +40,7 @@ class Client 'log_formatter' => \JsonRpc\Logging\LogstashFormatter::class, ]; $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->logger = $logger; } /** @@ -124,11 +114,11 @@ class Client try { $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'])) { $message = is_array($body['error']['message']) ? json_encode($body['error']['message']) : $body['error']['message']; throw new RpcServerException($message, $body['error']['code']); } + app('rpc.logger')->info('rpc call log new return'); return $body['result']; } catch (\InvalidArgumentException $e) { diff --git a/src/Providers/LoggerServiceProvider.php b/src/Providers/LoggerServiceProvider.php index 61cdbee..a420320 100644 --- a/src/Providers/LoggerServiceProvider.php +++ b/src/Providers/LoggerServiceProvider.php @@ -9,8 +9,32 @@ namespace JsonRpc\Providers; use Illuminate\Support\ServiceProvider; +use JsonRpc\Exception\RpcServerException; +use Monolog\Handler\StreamHandler; +use Monolog\Logger; 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); + }); + } + } \ No newline at end of file