Skip to content

Commit

Permalink
add debug log option (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon authored Dec 7, 2023
1 parent 5ea787d commit db7506b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/Jobs/PingInactiveConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Pusher\Event as PusherEvent;

class PingInactiveConnections
Expand All @@ -16,6 +17,8 @@ class PingInactiveConnections
*/
public function handle(ChannelManager $channels): void
{
Log::info('Pinging Inactive Connections');

$pusher = new PusherEvent($channels);

app(ApplicationProvider::class)
Expand All @@ -27,6 +30,8 @@ public function handle(ChannelManager $channels): void
}

$pusher->ping($connection->connection());

Log::info('Connection Pinged', $connection->id());
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions src/Jobs/PruneStaleConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Loggers\Log;

class PruneStaleConnections
{
Expand All @@ -15,6 +16,8 @@ class PruneStaleConnections
*/
public function handle(ChannelManager $channels): void
{
Log::info('Pruning Stale Connections');

app(ApplicationProvider::class)
->all()
->each(function ($application) use ($channels) {
Expand All @@ -36,6 +39,8 @@ public function handle(ChannelManager $channels): void
->unsubscribeFromAll($connection->connection());

$connection->disconnect();

Log::info('Connection Pruned', $connection->id());
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions src/Loggers/Log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Laravel\Reverb\Loggers;

use Laravel\Reverb\Contracts\Logger;

class Log
{
/**
* The logger instance.
*
* @var \Laravel\Reverb\Contracts\Logger
*/
protected static $logger;

/**
* Proxy method calls to the logger instance.
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
if (! static::$logger) {
static::$logger = app(Logger::class);
}

return static::$logger->$name(...$arguments);
}
}
20 changes: 18 additions & 2 deletions src/Pusher/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Exceptions\InvalidOrigin;
use Laravel\Reverb\Exceptions\PusherException;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Pusher\Event as PusherEvent;

class Server
Expand All @@ -29,6 +30,8 @@ public function open(Connection $connection): void
$connection->touch();

$this->pusher->handle($connection, 'pusher:connection_established');

Log::info('Connection Established', $connection->id());
} catch (Exception $e) {
$this->error($connection, $e);
}
Expand All @@ -39,6 +42,11 @@ public function open(Connection $connection): void
*/
public function message(Connection $from, string $message): void
{
Log::info('Message Received', $from->id());
Log::message($message);

$from->touch();

$event = json_decode($message, true);

try {
Expand All @@ -52,11 +60,11 @@ public function message(Connection $from, string $message): void
default => ClientEvent::handle($from, $event)
};

Log::info('Message Handled', $from->id());

} catch (Exception $e) {
$this->error($from, $e);
}

$from->touch();
}

/**
Expand All @@ -69,6 +77,8 @@ public function close(Connection $connection): void
->unsubscribeFromAll($connection);

$connection->disconnect();

Log::info('Connection Closed', $connection->id());
}

/**
Expand All @@ -79,6 +89,9 @@ public function error(Connection $connection, Exception $exception): void
if ($exception instanceof PusherException) {
$connection->send(json_encode($exception->payload()));

Log::error('Message from '.$connection->id().' resulted in a pusher error');
Log::info($exception->getMessage());

return;
}

Expand All @@ -89,6 +102,9 @@ public function error(Connection $connection, Exception $exception): void
'message' => 'Invalid message format',
]),
]));

Log::error('Message from '.$connection->id().' resulted in an unknown error');
Log::info($exception->getMessage());
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Servers/ApiGateway/Jobs/SendToConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Config;
use Laravel\Reverb\Loggers\Log;
use Throwable;

class SendToConnection implements ShouldQueue
Expand Down Expand Up @@ -36,7 +37,8 @@ public function handle(): void
'Data' => $this->message,
]);
} catch (Throwable $e) {
//
Log::error('Unable to send message.');
Log::info($e->getMessage());
}
}
}
9 changes: 8 additions & 1 deletion src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\Command;
use Laravel\Reverb\Concerns\InteractsWithAsyncRedis;
use Laravel\Reverb\Contracts\Logger;
use Laravel\Reverb\Jobs\PingInactiveConnections;
use Laravel\Reverb\Jobs\PruneStaleConnections;
use Laravel\Reverb\Loggers\CliLogger;
use Laravel\Reverb\Servers\Reverb\Factory as ServerFactory;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
Expand All @@ -21,7 +23,8 @@ class StartServer extends Command
*/
protected $signature = 'reverb:start
{--host=}
{--port=}';
{--port=}
{--debug}';

/**
* The console command description.
Expand All @@ -35,6 +38,10 @@ class StartServer extends Command
*/
public function handle(): void
{
if ($this->option('debug')) {
$this->laravel->instance(Logger::class, new CliLogger($this->output));
}

$config = $this->laravel['config']['reverb.servers.reverb'];
$host = $this->option('host') ?: $config['host'];
$port = $this->option('port') ?: $config['port'];
Expand Down
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Contracts\Logger;
use Laravel\Reverb\Loggers\NullLogger;

class ServiceProvider extends BaseServiceProvider
{
Expand All @@ -31,6 +33,8 @@ public function register()
__DIR__.'/../config/reverb.php', 'reverb'
);

$this->app->instance(Logger::class, new NullLogger);

$this->app->singleton(ServerManager::class);

$this->initializeServer();
Expand Down

0 comments on commit db7506b

Please sign in to comment.