Add logger
This commit is contained in:
@ -15,7 +15,8 @@
|
|||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"clue/socket-raw": "^1.5",
|
"clue/socket-raw": "^1.5",
|
||||||
"composer/semver": "^3.2",
|
"composer/semver": "^3.2",
|
||||||
"phpinnacle/buffer": "^1.2"
|
"phpinnacle/buffer": "^1.2",
|
||||||
|
"psr/log": "^1.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"ergebnis/composer-normalize": "9999999-dev",
|
"ergebnis/composer-normalize": "9999999-dev",
|
||||||
|
@ -6,6 +6,8 @@ namespace Nsq;
|
|||||||
|
|
||||||
use Composer\InstalledVersions;
|
use Composer\InstalledVersions;
|
||||||
use PHPinnacle\Buffer\ByteBuffer;
|
use PHPinnacle\Buffer\ByteBuffer;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Psr\Log\NullLogger;
|
||||||
use Socket\Raw\Factory;
|
use Socket\Raw\Factory;
|
||||||
use Socket\Raw\Socket;
|
use Socket\Raw\Socket;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
@ -35,6 +37,8 @@ abstract class Connection
|
|||||||
|
|
||||||
public ?Socket $socket = null;
|
public ?Socket $socket = null;
|
||||||
|
|
||||||
|
protected LoggerInterface $logger;
|
||||||
|
|
||||||
private bool $closed = false;
|
private bool $closed = false;
|
||||||
|
|
||||||
private Config $config;
|
private Config $config;
|
||||||
@ -46,6 +50,7 @@ abstract class Connection
|
|||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $address,
|
string $address,
|
||||||
|
LoggerInterface $logger = null,
|
||||||
string $clientId = null,
|
string $clientId = null,
|
||||||
string $hostname = null,
|
string $hostname = null,
|
||||||
string $userAgent = null
|
string $userAgent = null
|
||||||
@ -57,6 +62,8 @@ abstract class Connection
|
|||||||
'hostname' => $hostname ?? '',
|
'hostname' => $hostname ?? '',
|
||||||
'user_agent' => $userAgent ?? 'nsqphp/'.InstalledVersions::getPrettyVersion('nsq/nsq'),
|
'user_agent' => $userAgent ?? 'nsqphp/'.InstalledVersions::getPrettyVersion('nsq/nsq'),
|
||||||
];
|
];
|
||||||
|
|
||||||
|
$this->logger = $logger ?? new NullLogger();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function connect(): void
|
public function connect(): void
|
||||||
@ -67,6 +74,8 @@ abstract class Connection
|
|||||||
$body = json_encode($this->features, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
|
$body = json_encode($this->features, JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT);
|
||||||
$size = pack('N', \strlen($body));
|
$size = pack('N', \strlen($body));
|
||||||
|
|
||||||
|
$this->logger->info('Feature Negotiation: '.http_build_query($this->features));
|
||||||
|
|
||||||
$this->send('IDENTIFY '.PHP_EOL.$size.$body)->expectResponse(self::OK);
|
$this->send('IDENTIFY '.PHP_EOL.$size.$body)->expectResponse(self::OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +95,7 @@ abstract class Connection
|
|||||||
$this->socket->close();
|
$this->socket->close();
|
||||||
}
|
}
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
// Not interested
|
$this->logger->debug($e->getMessage(), ['exception' => $e]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->closed = true;
|
$this->closed = true;
|
||||||
@ -111,11 +120,15 @@ abstract class Connection
|
|||||||
{
|
{
|
||||||
$socket = $this->socket();
|
$socket = $this->socket();
|
||||||
|
|
||||||
|
$this->logger->debug('Send buffer: '.$buffer);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$socket->write($buffer);
|
$socket->write($buffer);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
$this->closed = true;
|
$this->closed = true;
|
||||||
|
|
||||||
|
$this->logger->error($e->getMessage(), ['exception' => $e]);
|
||||||
|
|
||||||
throw $e;
|
throw $e;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user