From 3eba9c8094f823f6f86aedd098b1743553dad7f6 Mon Sep 17 00:00:00 2001 From: dongwei <838456736@qq.com> Date: Thu, 10 Jan 2019 15:22:28 +0800 Subject: [PATCH] logstash --- src/Logging/LogstashFormatter.php | 57 +++++++++++++++++++++++++ src/Providers/ClientServiceProvider.php | 2 + 2 files changed, 59 insertions(+) create mode 100644 src/Logging/LogstashFormatter.php diff --git a/src/Logging/LogstashFormatter.php b/src/Logging/LogstashFormatter.php new file mode 100644 index 0000000..e485fce --- /dev/null +++ b/src/Logging/LogstashFormatter.php @@ -0,0 +1,57 @@ +hostname = gethostname(); + } + + public function format(array $record) + { + $record = parent::format($record); + + $message = array( + '@timestamp' => $record['datetime'], + 'host' => $this->hostname, + 'app' => env('APP_NAME'), + 'env' => app()->environment(), + ); + + $request_id = app('request')->header('X-Request-Id'); + if ($request_id) { + $message['request_id'] = $request_id; + } + + // if (isset($record['channel'])) { + // $message['channel'] = $record['channel']; + // } + + if (isset($record['level_name'])) { + $message['level'] = $record['level_name']; + } + if (isset($record['message'])) { + $message['message'] = $record['message']; + } + + if (!empty($record['context'])) { + $message['context'] = $this->toJson($record['context']); + } + + return $this->toJson($message) . "\n"; + } +} diff --git a/src/Providers/ClientServiceProvider.php b/src/Providers/ClientServiceProvider.php index bb4a784..5838110 100644 --- a/src/Providers/ClientServiceProvider.php +++ b/src/Providers/ClientServiceProvider.php @@ -4,6 +4,7 @@ namespace JsonRpc\Providers; use Illuminate\Support\ServiceProvider; use JsonRpc\Client; +use JsonRpc\Logging\LogstashFormatter; use Monolog\Formatter\LineFormatter; use Monolog\Handler\StreamHandler; use Monolog\Logger; @@ -21,6 +22,7 @@ class ClientServiceProvider extends ServiceProvider $config = config('rpc.client'); $stream = new StreamHandler($this->app->storagePath()."/logs/rpc_monitor_".date("Ymd").".log"); + $stream->setFormatter(LogstashFormatter::class); $logger = new Logger('RPC.LOGGER'); $logger->pushHandler($stream); $this->app->singleton('rpc', function () use ($config, $logger) {