From f2d3baebbdf082aa739eea7766ad6174895bd8c1 Mon Sep 17 00:00:00 2001 From: Marcel Pociot Date: Thu, 6 Oct 2022 20:41:35 +0200 Subject: [PATCH] Improve logging/verbose CLI output --- src/API/TriggerEvent.php | 3 ++- src/Console/Commands/StartServer.php | 7 +++++-- src/Server/Loggers/ConnectionLogger.php | 8 ++++++-- src/Server/Loggers/WebSocketsLogger.php | 9 +++++---- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/API/TriggerEvent.php b/src/API/TriggerEvent.php index b5120278b1..4e293534be 100644 --- a/src/API/TriggerEvent.php +++ b/src/API/TriggerEvent.php @@ -6,6 +6,7 @@ use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector; use Illuminate\Http\Request; use React\Promise\Deferred; +use React\Promise\PromiseInterface; class TriggerEvent extends Controller { @@ -13,7 +14,7 @@ class TriggerEvent extends Controller * Handle the incoming request. * * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response + * @return PromiseInterface */ public function __invoke(Request $request) { diff --git a/src/Console/Commands/StartServer.php b/src/Console/Commands/StartServer.php index 69ea1ffe47..429b478ffd 100644 --- a/src/Console/Commands/StartServer.php +++ b/src/Console/Commands/StartServer.php @@ -13,6 +13,7 @@ use Illuminate\Support\Facades\Cache; use React\EventLoop\Factory as LoopFactory; use React\EventLoop\LoopInterface; +use Symfony\Component\Console\Output\OutputInterface; use function React\Promise\all; class StartServer extends Command @@ -134,7 +135,7 @@ protected function configureStatistics() $intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600); $this->loop->addPeriodicTimer($intervalInSeconds, function () { - $this->line('Saving statistics...'); + $this->line('Saving statistics...', null, OutputInterface::VERBOSITY_VERBOSE); StatisticsCollectorFacade::save(); }); @@ -260,7 +261,9 @@ protected function configureConnectionLogger() */ protected function startServer() { - $this->info("Starting the WebSocket server on port {$this->option('port')}..."); + $this->components->info("Starting the WebSocket server on port {$this->option('port')}..."); + $this->comment(' Press Ctrl+C to stop the server'); + $this->newLine(); $this->buildServer(); diff --git a/src/Server/Loggers/ConnectionLogger.php b/src/Server/Loggers/ConnectionLogger.php index 60e2ffbe1f..62d2bb53da 100644 --- a/src/Server/Loggers/ConnectionLogger.php +++ b/src/Server/Loggers/ConnectionLogger.php @@ -48,8 +48,9 @@ public function setConnection(ConnectionInterface $connection) public function send($data) { $socketId = $this->connection->socketId ?? null; + $appId = $this->connection->app->id ?? null; - $this->info("Connection id {$socketId} sending message {$data}"); + $this->info("[{$appId}][{$socketId}] Sending message ". ($this->verbose ? $data : '')); $this->connection->send($data); } @@ -61,7 +62,10 @@ public function send($data) */ public function close() { - $this->warn("Connection id {$this->connection->socketId} closing."); + $socketId = $this->connection->socketId ?? null; + $appId = $this->connection->app->id ?? null; + + $this->warn("[{$appId}][{$socketId}] Closing connection"); $this->connection->close(); } diff --git a/src/Server/Loggers/WebSocketsLogger.php b/src/Server/Loggers/WebSocketsLogger.php index a9555e1f13..56e7c1a84c 100644 --- a/src/Server/Loggers/WebSocketsLogger.php +++ b/src/Server/Loggers/WebSocketsLogger.php @@ -53,7 +53,7 @@ public function onOpen(ConnectionInterface $connection) { $appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); - $this->warn("New connection opened for app key {$appKey}."); + $this->info("[$appKey] New connection opened."); $this->app->onOpen(ConnectionLogger::decorate($connection)); } @@ -67,7 +67,7 @@ public function onOpen(ConnectionInterface $connection) */ public function onMessage(ConnectionInterface $connection, MessageInterface $message) { - $this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}."); + $this->info("[{$connection->app->id}][{$connection->socketId}] Received message ". ($this->verbose ? $message->getPayload() : '')); $this->app->onMessage(ConnectionLogger::decorate($connection), $message); } @@ -81,8 +81,9 @@ public function onMessage(ConnectionInterface $connection, MessageInterface $mes public function onClose(ConnectionInterface $connection) { $socketId = $connection->socketId ?? null; + $appId = $connection->app->id ?? null; - $this->warn("Connection id {$socketId} closed."); + $this->warn("[{$appId}][{$socketId}] Connection closed"); $this->app->onClose(ConnectionLogger::decorate($connection)); } @@ -100,7 +101,7 @@ public function onError(ConnectionInterface $connection, Exception $exception) $appId = $connection->app->id ?? 'Unknown app id'; - $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; + $message = "[{$appId}] Exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`"; if ($this->verbose) { $message .= $exception->getTraceAsString();