Skip to content

Added LoggerAwareInterface support and abstract implementation #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Application/LoggerAwareApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Bloatless\WebSocket\Application;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

abstract class LoggerAwareApplication extends Application implements LoggerAwareInterface {
private LoggerInterface $logger;

protected function __construct()
{
parent::__construct();

$this->logger = new NullLogger();
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

protected function getLogger(): LoggerInterface
{
return $this->logger;
}
}
3 changes: 2 additions & 1 deletion src/Application/StatusApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Bloatless\WebSocket\Connection;

class StatusApplication extends Application
class StatusApplication extends LoggerAwareApplication
{
/**
* Holds client connected to the status application.
Expand Down Expand Up @@ -164,6 +164,7 @@ public function statusMsg(string $text, string $type = 'info'): void
];
$encodedData = $this->encodeData('statusMsg', $data);
$this->sendAll($encodedData);
$this->getLogger()->log($type, 'Status message: ' . $text);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Bloatless\WebSocket;

use Bloatless\WebSocket\Application\ApplicationInterface;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -142,7 +143,7 @@ public function run(): void

while (true) {
$this->timers->runAll();

$changed_sockets = $this->allsockets;
@stream_select($changed_sockets, $write, $except, 0, 5000);
foreach ($changed_sockets as $socket) {
Expand Down Expand Up @@ -247,6 +248,10 @@ public function registerApplication(string $key, ApplicationInterface $applicati
{
$this->applications[$key] = $application;

if ($application instanceof LoggerAwareInterface) {
$application->setLogger($this->logger);
}

// status is kind of a system-app, needs some special cases:
if ($key === 'status') {
$serverInfo = array(
Expand Down