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

Commit

Permalink
Improve logging/verbose CLI output
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Oct 6, 2022
1 parent fb958fb commit f2d3bae
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/API/TriggerEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector;
use Illuminate\Http\Request;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;

class TriggerEvent extends Controller
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @return PromiseInterface
*/
public function __invoke(Request $request)
{
Expand Down
7 changes: 5 additions & 2 deletions src/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
});
Expand Down Expand Up @@ -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(' <fg=yellow;options=bold>Press Ctrl+C to stop the server</>');
$this->newLine();

$this->buildServer();

Expand Down
8 changes: 6 additions & 2 deletions src/Server/Loggers/ConnectionLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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();
}
Expand Down
9 changes: 5 additions & 4 deletions src/Server/Loggers/WebSocketsLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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);
}
Expand All @@ -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));
}
Expand All @@ -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();
Expand Down

0 comments on commit f2d3bae

Please sign in to comment.