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) {