Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit f2d3bae

Browse files
committed
Improve logging/verbose CLI output
1 parent fb958fb commit f2d3bae

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

src/API/TriggerEvent.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector;
77
use Illuminate\Http\Request;
88
use React\Promise\Deferred;
9+
use React\Promise\PromiseInterface;
910

1011
class TriggerEvent extends Controller
1112
{
1213
/**
1314
* Handle the incoming request.
1415
*
1516
* @param \Illuminate\Http\Request $request
16-
* @return \Illuminate\Http\Response
17+
* @return PromiseInterface
1718
*/
1819
public function __invoke(Request $request)
1920
{

src/Console/Commands/StartServer.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Illuminate\Support\Facades\Cache;
1414
use React\EventLoop\Factory as LoopFactory;
1515
use React\EventLoop\LoopInterface;
16+
use Symfony\Component\Console\Output\OutputInterface;
1617
use function React\Promise\all;
1718

1819
class StartServer extends Command
@@ -134,7 +135,7 @@ protected function configureStatistics()
134135
$intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600);
135136

136137
$this->loop->addPeriodicTimer($intervalInSeconds, function () {
137-
$this->line('Saving statistics...');
138+
$this->line('Saving statistics...', null, OutputInterface::VERBOSITY_VERBOSE);
138139

139140
StatisticsCollectorFacade::save();
140141
});
@@ -260,7 +261,9 @@ protected function configureConnectionLogger()
260261
*/
261262
protected function startServer()
262263
{
263-
$this->info("Starting the WebSocket server on port {$this->option('port')}...");
264+
$this->components->info("Starting the WebSocket server on port {$this->option('port')}...");
265+
$this->comment(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');
266+
$this->newLine();
264267

265268
$this->buildServer();
266269

src/Server/Loggers/ConnectionLogger.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ public function setConnection(ConnectionInterface $connection)
4848
public function send($data)
4949
{
5050
$socketId = $this->connection->socketId ?? null;
51+
$appId = $this->connection->app->id ?? null;
5152

52-
$this->info("Connection id {$socketId} sending message {$data}");
53+
$this->info("[{$appId}][{$socketId}] Sending message ". ($this->verbose ? $data : ''));
5354

5455
$this->connection->send($data);
5556
}
@@ -61,7 +62,10 @@ public function send($data)
6162
*/
6263
public function close()
6364
{
64-
$this->warn("Connection id {$this->connection->socketId} closing.");
65+
$socketId = $this->connection->socketId ?? null;
66+
$appId = $this->connection->app->id ?? null;
67+
68+
$this->warn("[{$appId}][{$socketId}] Closing connection");
6569

6670
$this->connection->close();
6771
}

src/Server/Loggers/WebSocketsLogger.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function onOpen(ConnectionInterface $connection)
5353
{
5454
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey');
5555

56-
$this->warn("New connection opened for app key {$appKey}.");
56+
$this->info("[$appKey] New connection opened.");
5757

5858
$this->app->onOpen(ConnectionLogger::decorate($connection));
5959
}
@@ -67,7 +67,7 @@ public function onOpen(ConnectionInterface $connection)
6767
*/
6868
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
6969
{
70-
$this->info("{$connection->app->id}: connection id {$connection->socketId} received message: {$message->getPayload()}.");
70+
$this->info("[{$connection->app->id}][{$connection->socketId}] Received message ". ($this->verbose ? $message->getPayload() : ''));
7171

7272
$this->app->onMessage(ConnectionLogger::decorate($connection), $message);
7373
}
@@ -81,8 +81,9 @@ public function onMessage(ConnectionInterface $connection, MessageInterface $mes
8181
public function onClose(ConnectionInterface $connection)
8282
{
8383
$socketId = $connection->socketId ?? null;
84+
$appId = $connection->app->id ?? null;
8485

85-
$this->warn("Connection id {$socketId} closed.");
86+
$this->warn("[{$appId}][{$socketId}] Connection closed");
8687

8788
$this->app->onClose(ConnectionLogger::decorate($connection));
8889
}
@@ -100,7 +101,7 @@ public function onError(ConnectionInterface $connection, Exception $exception)
100101

101102
$appId = $connection->app->id ?? 'Unknown app id';
102103

103-
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`.";
104+
$message = "[{$appId}] Exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
104105

105106
if ($this->verbose) {
106107
$message .= $exception->getTraceAsString();

0 commit comments

Comments
 (0)