Improve logging/verbose CLI output

This commit is contained in:
Marcel Pociot 2022-10-06 20:41:35 +02:00
parent fb958fb851
commit f2d3baebbd
4 changed files with 18 additions and 9 deletions

View File

@ -6,6 +6,7 @@ use BeyondCode\LaravelWebSockets\DashboardLogger;
use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector; use BeyondCode\LaravelWebSockets\Facades\StatisticsCollector;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use React\Promise\Deferred; use React\Promise\Deferred;
use React\Promise\PromiseInterface;
class TriggerEvent extends Controller class TriggerEvent extends Controller
{ {
@ -13,7 +14,7 @@ class TriggerEvent extends Controller
* Handle the incoming request. * Handle the incoming request.
* *
* @param \Illuminate\Http\Request $request * @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response * @return PromiseInterface
*/ */
public function __invoke(Request $request) public function __invoke(Request $request)
{ {

View File

@ -13,6 +13,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use React\EventLoop\Factory as LoopFactory; use React\EventLoop\Factory as LoopFactory;
use React\EventLoop\LoopInterface; use React\EventLoop\LoopInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function React\Promise\all; use function React\Promise\all;
class StartServer extends Command class StartServer extends Command
@ -134,7 +135,7 @@ class StartServer extends Command
$intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600); $intervalInSeconds = $this->option('statistics-interval') ?: config('websockets.statistics.interval_in_seconds', 3600);
$this->loop->addPeriodicTimer($intervalInSeconds, function () { $this->loop->addPeriodicTimer($intervalInSeconds, function () {
$this->line('Saving statistics...'); $this->line('Saving statistics...', null, OutputInterface::VERBOSITY_VERBOSE);
StatisticsCollectorFacade::save(); StatisticsCollectorFacade::save();
}); });
@ -260,7 +261,9 @@ class StartServer extends Command
*/ */
protected function startServer() 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(); $this->buildServer();

View File

@ -48,8 +48,9 @@ class ConnectionLogger extends Logger implements ConnectionInterface
public function send($data) public function send($data)
{ {
$socketId = $this->connection->socketId ?? null; $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); $this->connection->send($data);
} }
@ -61,7 +62,10 @@ class ConnectionLogger extends Logger implements ConnectionInterface
*/ */
public function close() 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(); $this->connection->close();
} }

View File

@ -53,7 +53,7 @@ class WebSocketsLogger extends Logger implements MessageComponentInterface
{ {
$appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); $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)); $this->app->onOpen(ConnectionLogger::decorate($connection));
} }
@ -67,7 +67,7 @@ class WebSocketsLogger extends Logger implements MessageComponentInterface
*/ */
public function onMessage(ConnectionInterface $connection, MessageInterface $message) 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); $this->app->onMessage(ConnectionLogger::decorate($connection), $message);
} }
@ -81,8 +81,9 @@ class WebSocketsLogger extends Logger implements MessageComponentInterface
public function onClose(ConnectionInterface $connection) public function onClose(ConnectionInterface $connection)
{ {
$socketId = $connection->socketId ?? null; $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)); $this->app->onClose(ConnectionLogger::decorate($connection));
} }
@ -100,7 +101,7 @@ class WebSocketsLogger extends Logger implements MessageComponentInterface
$appId = $connection->app->id ?? 'Unknown app id'; $appId = $connection->app->id ?? 'Unknown app id';
$message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`."; $message = "[{$appId}] Exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
if ($this->verbose) { if ($this->verbose) {
$message .= $exception->getTraceAsString(); $message .= $exception->getTraceAsString();