add rpc logger provider
This commit is contained in:
parent
5fd6097604
commit
3e99169d00
|
@ -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) {
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user