mirror of
https://github.com/friendica/friendica
synced 2025-04-23 22:30:10 +00:00
Adding worker id & functionname to worker logs
This commit is contained in:
parent
2d91d5c3d9
commit
ee37632695
3 changed files with 346 additions and 12 deletions
119
tests/src/Util/Logger/WorkerLoggerTest.php
Normal file
119
tests/src/Util/Logger/WorkerLoggerTest.php
Normal file
|
@ -0,0 +1,119 @@
|
|||
<?php
|
||||
|
||||
namespace src\Util\Logger;
|
||||
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Util\Logger\WorkerLogger;
|
||||
|
||||
class WorkerLoggerTest extends MockedTest
|
||||
{
|
||||
private function assertUid($uid, $length = 7)
|
||||
{
|
||||
$this->assertRegExp('/^[a-zA-Z0-9]{' . $length . '}+$/', $uid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the a id with length zero
|
||||
* @expectedException
|
||||
*/
|
||||
public function testGetWorkerIdZero()
|
||||
{
|
||||
$logger = \Mockery::mock('Psr\Log\LoggerInterface');
|
||||
$logger
|
||||
->shouldReceive('alert')
|
||||
->with('id length must be greater than 0.')
|
||||
->once();
|
||||
new WorkerLogger($logger, 'test', 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the generated Uid
|
||||
*/
|
||||
public function testGetWorkerId()
|
||||
{
|
||||
$logger = \Mockery::mock('Psr\Log\LoggerInterface');
|
||||
for ($i = 1; $i < 14; $i++) {
|
||||
$workLogger = new WorkerLogger($logger, 'test', $i);
|
||||
$uid = $workLogger->getWorkerId();
|
||||
$this->assertUid($uid, $i);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataTest()
|
||||
{
|
||||
return [
|
||||
'info' => [
|
||||
'func' => 'info',
|
||||
'msg' => 'the alert',
|
||||
'context' => [],
|
||||
],
|
||||
'alert' => [
|
||||
'func' => 'alert',
|
||||
'msg' => 'another alert',
|
||||
'context' => ['test' => 'it'],
|
||||
],
|
||||
'critical' => [
|
||||
'func' => 'critical',
|
||||
'msg' => 'Critical msg used',
|
||||
'context' => ['test' => 'it', 'more' => 0.24545],
|
||||
],
|
||||
'error' => [
|
||||
'func' => 'error',
|
||||
'msg' => 21345623,
|
||||
'context' => ['test' => 'it', 'yet' => true],
|
||||
],
|
||||
'warning' => [
|
||||
'func' => 'warning',
|
||||
'msg' => 'another alert' . 123523 . 324.54534 . 'test',
|
||||
'context' => ['test' => 'it', 2 => 'nope'],
|
||||
],
|
||||
'notice' => [
|
||||
'func' => 'notice',
|
||||
'msg' => 'Notice' . ' alert' . true . 'with' . '\'strange\'' . 1.24. 'behavior',
|
||||
'context' => ['test' => 'it'],
|
||||
],
|
||||
'debug' => [
|
||||
'func' => 'debug',
|
||||
'msg' => 'at last a debug',
|
||||
'context' => ['test' => 'it'],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the WorkerLogger with different log calls
|
||||
* @dataProvider dataTest
|
||||
*/
|
||||
public function testEmergency($func, $msg, $context = [])
|
||||
{
|
||||
$logger = \Mockery::mock('Psr\Log\LoggerInterface');
|
||||
$workLogger = new WorkerLogger($logger, 'test');
|
||||
$testContext = $context;
|
||||
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||
$testContext['worker_cmd'] = 'test';
|
||||
$this->assertUid($testContext['worker_id']);
|
||||
$logger
|
||||
->shouldReceive($func)
|
||||
->with($msg, $testContext)
|
||||
->once();
|
||||
$workLogger->$func($msg, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the WorkerLogger with
|
||||
*/
|
||||
public function testLog()
|
||||
{
|
||||
$logger = \Mockery::mock('Psr\Log\LoggerInterface');
|
||||
$workLogger = new WorkerLogger($logger, 'test');
|
||||
$context = $testContext = ['test' => 'it'];
|
||||
$testContext['worker_id'] = $workLogger->getWorkerId();
|
||||
$testContext['worker_cmd'] = 'test';
|
||||
$this->assertUid($testContext['worker_id']);
|
||||
$logger
|
||||
->shouldReceive('log')
|
||||
->with('debug', 'a test', $testContext)
|
||||
->once();
|
||||
$workLogger->log('debug', 'a test', $context);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue