Add Unique-Id for each worker execution
This commit is contained in:
parent
ec25fd2b63
commit
9a5e6642b6
|
@ -31,6 +31,9 @@ use Psr\Log\LoggerInterface;
|
||||||
*/
|
*/
|
||||||
class WorkerLogger implements LoggerInterface
|
class WorkerLogger implements LoggerInterface
|
||||||
{
|
{
|
||||||
|
/** @var int Length of the unique worker id */
|
||||||
|
const WORKER_ID_LENGTH = 7;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var LoggerInterface The original Logger instance
|
* @var LoggerInterface The original Logger instance
|
||||||
*/
|
*/
|
||||||
|
@ -48,17 +51,14 @@ class WorkerLogger implements LoggerInterface
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param LoggerInterface $logger The logger for worker entries
|
* @param LoggerInterface $logger The logger for worker entries
|
||||||
* @param string $functionName The current function name of the worker
|
|
||||||
* @param int $idLength The length of the generated worker ID
|
|
||||||
*
|
*
|
||||||
* @throws LoggerException
|
* @throws LoggerException
|
||||||
*/
|
*/
|
||||||
public function __construct(LoggerInterface $logger, string $functionName = '', int $idLength = 7)
|
public function __construct(LoggerInterface $logger)
|
||||||
{
|
{
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->functionName = $functionName;
|
|
||||||
try {
|
try {
|
||||||
$this->workerId = Strings::getRandomHex($idLength);
|
$this->workerId = Strings::getRandomHex(self::WORKER_ID_LENGTH);
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
throw new LoggerException('Cannot generate random Hex.', $exception);
|
throw new LoggerException('Cannot generate random Hex.', $exception);
|
||||||
}
|
}
|
||||||
|
@ -68,10 +68,17 @@ class WorkerLogger implements LoggerInterface
|
||||||
* Sets the function name for additional logging
|
* Sets the function name for additional logging
|
||||||
*
|
*
|
||||||
* @param string $functionName
|
* @param string $functionName
|
||||||
|
*
|
||||||
|
* @throws LoggerException
|
||||||
*/
|
*/
|
||||||
public function setFunctionName(string $functionName)
|
public function setFunctionName(string $functionName)
|
||||||
{
|
{
|
||||||
$this->functionName = $functionName;
|
$this->functionName = $functionName;
|
||||||
|
try {
|
||||||
|
$this->workerId = Strings::getRandomHex(self::WORKER_ID_LENGTH);
|
||||||
|
} catch (\Exception $exception) {
|
||||||
|
throw new LoggerException('Cannot generate random Hex.', $exception);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -27,34 +27,9 @@ use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
class WorkerLoggerTest extends MockedTest
|
class WorkerLoggerTest extends MockedTest
|
||||||
{
|
{
|
||||||
private function assertUid($uid, $length = 7)
|
private function assertUid($uid)
|
||||||
{
|
{
|
||||||
self::assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
|
self::assertRegExp('/^[a-zA-Z0-9]{' . WorkerLogger::WORKER_ID_LENGTH . '}+$/', $uid);
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the a id with length zero
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function testGetWorkerIdZero()
|
|
||||||
{
|
|
||||||
$this->expectException(\Error::class);
|
|
||||||
|
|
||||||
$logger = \Mockery::mock(LoggerInterface::class);
|
|
||||||
new WorkerLogger($logger, 'test', 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test the generated Uid
|
|
||||||
*/
|
|
||||||
public function testGetWorkerId()
|
|
||||||
{
|
|
||||||
$logger = \Mockery::mock(LoggerInterface::class);
|
|
||||||
for ($i = 1; $i < 14; $i++) {
|
|
||||||
$workLogger = new WorkerLogger($logger, 'test', $i);
|
|
||||||
$uid = $workLogger->getWorkerId();
|
|
||||||
self::assertUid($uid, $i);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function dataTest()
|
public function dataTest()
|
||||||
|
@ -105,10 +80,10 @@ class WorkerLoggerTest extends MockedTest
|
||||||
public function testEmergency($func, $msg, $context = [])
|
public function testEmergency($func, $msg, $context = [])
|
||||||
{
|
{
|
||||||
$logger = \Mockery::mock(LoggerInterface::class);
|
$logger = \Mockery::mock(LoggerInterface::class);
|
||||||
$workLogger = new WorkerLogger($logger, 'test');
|
$workLogger = new WorkerLogger($logger);
|
||||||
$testContext = $context;
|
$testContext = $context;
|
||||||
$testContext['worker_id'] = $workLogger->getWorkerId();
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||||
$testContext['worker_cmd'] = 'test';
|
$testContext['worker_cmd'] = '';
|
||||||
self::assertUid($testContext['worker_id']);
|
self::assertUid($testContext['worker_id']);
|
||||||
$logger
|
$logger
|
||||||
->shouldReceive($func)
|
->shouldReceive($func)
|
||||||
|
@ -123,10 +98,43 @@ class WorkerLoggerTest extends MockedTest
|
||||||
public function testLog()
|
public function testLog()
|
||||||
{
|
{
|
||||||
$logger = \Mockery::mock(LoggerInterface::class);
|
$logger = \Mockery::mock(LoggerInterface::class);
|
||||||
$workLogger = new WorkerLogger($logger, 'test');
|
$workLogger = new WorkerLogger($logger);
|
||||||
$context = $testContext = ['test' => 'it'];
|
$context = $testContext = ['test' => 'it'];
|
||||||
$testContext['worker_id'] = $workLogger->getWorkerId();
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||||
$testContext['worker_cmd'] = 'test';
|
$testContext['worker_cmd'] = '';
|
||||||
|
self::assertUid($testContext['worker_id']);
|
||||||
|
$logger
|
||||||
|
->shouldReceive('log')
|
||||||
|
->with('debug', 'a test', $testContext)
|
||||||
|
->once();
|
||||||
|
$workLogger->log('debug', 'a test', $context);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test the WorkerLogger after setting a worker function
|
||||||
|
*/
|
||||||
|
public function testChangedId()
|
||||||
|
{
|
||||||
|
$logger = \Mockery::mock(LoggerInterface::class);
|
||||||
|
$workLogger = new WorkerLogger($logger);
|
||||||
|
$context = $testContext = ['test' => 'it'];
|
||||||
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||||
|
$testContext['worker_cmd'] = '';
|
||||||
|
self::assertUid($testContext['worker_id']);
|
||||||
|
$logger
|
||||||
|
->shouldReceive('log')
|
||||||
|
->with('debug', 'a test', $testContext)
|
||||||
|
->once();
|
||||||
|
$workLogger->log('debug', 'a test', $context);
|
||||||
|
|
||||||
|
$workLogger->setFunctionName('testFunc');
|
||||||
|
|
||||||
|
self::assertNotEquals($testContext['worker_id'], $workLogger->getWorkerId());
|
||||||
|
|
||||||
|
$context = $testContext = ['test' => 'it'];
|
||||||
|
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||||
|
$testContext['worker_cmd'] = 'testFunc';
|
||||||
self::assertUid($testContext['worker_id']);
|
self::assertUid($testContext['worker_id']);
|
||||||
$logger
|
$logger
|
||||||
->shouldReceive('log')
|
->shouldReceive('log')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user