remove X-Client-App to logger format

This commit is contained in:
dongwei 2019-01-21 14:32:47 +08:00
parent dbfcab3491
commit f6f34a5b43
5 changed files with 6 additions and 150 deletions

View File

@ -82,7 +82,6 @@ return [
return [ return [
'app' => env('APP_NAME'), 'app' => env('APP_NAME'),
'log_path' => "/logs/rpc-".date("Y-m-d").".log",//rpc日志路径 'log_path' => "/logs/rpc-".date("Y-m-d").".log",//rpc日志路径
'log_formatter' => \App\Logging\LogstashFormatter::class, //rpc日志格式
// json rpc server 配置 // json rpc server 配置
'server' => [ 'server' => [
'name' => env('APP_NAME'), 'name' => env('APP_NAME'),

View File

@ -95,7 +95,7 @@ class Client
try { try {
$headers = [ $headers = [
'x-client-app' => $this->config['app'], 'X-Client-App' => $this->config['app'],
]; ];
app('rpc.logger')->info("client_request",array_merge($this->server_config, $payload)); 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', [

View File

@ -24,12 +24,12 @@ class LogstashFormatter extends NormalizerFormatter
public function format(array $record) public function format(array $record)
{ {
$record = parent::format($record); $record = parent::format($record);
$message = array( $message = array(
'@timestamp' => $record['datetime'], '@timestamp' => $record['datetime'],
'host' => $this->hostname, 'host' => $this->hostname,
'app' => env('APP_NAME'), 'app' => env('APP_NAME'),
'env' => app()->environment(), 'env' => app()->environment(),
'client_app' => app('request')->header('X-Client-App'),
); );
$request_id = app('request')->header('X-Request-Id'); $request_id = app('request')->header('X-Request-Id');
@ -41,6 +41,9 @@ class LogstashFormatter extends NormalizerFormatter
// $message['channel'] = $record['channel']; // $message['channel'] = $record['channel'];
// } // }
if (isset($record['x-client-app'])) {
$message['client_app'] = $record['x_client_app'];
}
if (isset($record['level_name'])) { if (isset($record['level_name'])) {
$message['level'] = $record['level_name']; $message['level'] = $record['level_name'];
} }

View File

@ -61,8 +61,7 @@ class JsonRpcServer
if (!$this->isEnoughParameter($class, $function, $params)) { if (!$this->isEnoughParameter($class, $function, $params)) {
return $this->error(self::Rpc_Error_Invalid_Params); return $this->error(self::Rpc_Error_Invalid_Params);
} }
app('rpc.logger')->info('server', [$id, $class,$method, $params]);
app('rpc.logger')->info('server', [$id, $class,$method, $params, $this->request->header('x-client-app')]);
$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]); app('rpc.logger')->info('server_result', [$id, $ret]);

View File

@ -1,145 +0,0 @@
<?php
namespace JsonRpc\Server;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class JsonRpcServer
{
const Rpc_Error_Parse_Error = -32700;
const Rpc_Error_Invalid_Request = -32600;
const Rpc_Error_NOT_FOUND = -32601;
const Rpc_Error_Invalid_Params = -32602;
const Rpc_Error_Internal_Error = -32603;
const Rpc_Success = 0;
const ErrorMsg = [
self::Rpc_Error_NOT_FOUND => 'Method not found',
self::Rpc_Error_Parse_Error => 'Json parse error',
self::Rpc_Error_Invalid_Request => 'Invalid request',
self::Rpc_Error_Invalid_Params => 'Invalid params',
self::Rpc_Success => 'Success'
];
/**
* @var Request
*/
public $request;
public function __construct($config)
{
$this->config = $config;
$this->request = function_exists('app') ? app('request') : Request::capture();
$this->map = require_once $config['map'];
}
public function handler()
{
if ($this->request->getContentType() != 'json') {
return $this->error(self::Rpc_Error_Invalid_Request);
}
try {
if ($this->request->method() == Request::METHOD_GET) {
$method = $this->request->input('method');
$id = $this->request->input('id');
$params = \GuzzleHttp\json_decode($this->request->input('params'),true);
} else {
list($method, $params, $id) = $this->parseJson($this->request->getContent());
}
list($class, $function) = $this->parseMethodWithMap($method);
if (!class_exists($class) || !method_exists($class, $function)) {
return $this->error(self::Rpc_Error_NOT_FOUND);
}
if (!$this->isEnoughParameter($class, $function, $params)) {
return $this->error(self::Rpc_Error_Invalid_Params);
}
app('rpc.logger')->info('server', [$id, $class,$method, $params, $this->request->header('x-client-app')]);
$ret = call_user_func_array([(new $class($id, $this->request)), $function], $params);
app('rpc.logger')->info('server_result', [$id, $ret]);
return JsonResponse::create($ret);
} catch (\InvalidArgumentException $e) {
return $this->error(self::Rpc_Error_Parse_Error);
}
}
protected function parseJson($data)
{
$data = \GuzzleHttp\json_decode($data, true);
$method = $data['method'];
$params = $data['params'];
$id = $data['id'];
return [$method, $params, $id];
}
protected function parseMethodWithMap($method)
{
return isset($this->map[$method]) ? $this->map[$method] : ['', ''];
}
/**
* thisis
* @param string $method 参数名称
* @return array 返回结果
*/
protected function parseMethod($method)
{
$method = explode('.', $method);
if (count($method) < 2) {
return ['', ''];
}
$function = array_pop($method);
$class = 'Rpc' . ucwords(array_pop($method));
foreach ($method as $one) {
$class = ucwords($one) . '\\' . $class;
}
$class = "App\Rpc\\$class";
return [$class, $function];
}
protected function isEnoughParameter($class, $method, $parameters)
{
$r = new \ReflectionMethod($class, $method);
$params = $r->getParameters();
$n = 0;
foreach ($params as $param) {
//$param is an instance of ReflectionParameter
if (!$param->isOptional()) {
$n++;
}
}
return count($parameters) >= $n;
}
protected function error($code, $msg = null, $id = null)
{
if ($msg === null) {
$msg = isset(self::ErrorMsg[$code]) ? self::ErrorMsg[$code] : 'undefined';
}
return JsonResponse::create([
'jsonrpc' => '2.0',
'error' => [
'code' => $code,
'message' => $msg,
],
'id' => $id
]);
}
}