use LoggerFactory
This commit is contained in:
parent
fa142578d8
commit
b69b3d206c
@ -20,7 +20,8 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"guzzlehttp/guzzle": "^7.0",
|
"guzzlehttp/guzzle": "^7.0",
|
||||||
"monolog/monolog": "^1.20",
|
"monolog/monolog": "^1.20",
|
||||||
"d3/sensitive-message-formatter": "^1.0"
|
"d3/sensitive-message-formatter": "^1.0",
|
||||||
|
"d3/logger-factory": "^1.0"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^10.5",
|
"phpunit/phpunit": "^10.5",
|
||||||
|
@ -17,9 +17,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace D3\GuzzleFactory\Apps;
|
namespace D3\GuzzleFactory\Apps;
|
||||||
|
|
||||||
|
use D3\LoggerFactory\LoggerFactory;
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Monolog\Handler\StreamHandler;
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use OxidEsales\Eshop\Core\Registry;
|
use OxidEsales\Eshop\Core\Registry;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
@ -46,25 +46,23 @@ trait OxidLoggerTrait
|
|||||||
?int $maxFiles = null
|
?int $maxFiles = null
|
||||||
): void
|
): void
|
||||||
{
|
{
|
||||||
if (!class_exists(Registry::class)) {
|
|
||||||
throw new RuntimeException(__METHOD__.' can executed in OXID eShop installations only');
|
|
||||||
}
|
|
||||||
|
|
||||||
$logger = new Logger($loggerName);
|
|
||||||
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
|
||||||
$logger->pushHandler($stream_handler);
|
|
||||||
|
|
||||||
$oxidLogFilePath = $this->getOxidLogPath('oxideshop.log');
|
|
||||||
$oxidStreamHandler = new StreamHandler($oxidLogFilePath, Logger::ERROR);
|
|
||||||
$logger->pushHandler($oxidStreamHandler);
|
|
||||||
|
|
||||||
if (isset($this->loggers['oxid'])) {
|
if (isset($this->loggers['oxid'])) {
|
||||||
unset($this->loggers['oxid']);
|
unset($this->loggers['oxid']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->loggers[$loggerName] = $logger;
|
$this->loggers[$loggerName] = $this->getLoggerFactory()->getCombinedOxidAndFileLogger(
|
||||||
|
$loggerName,
|
||||||
|
$filePath,
|
||||||
|
$logLevel,
|
||||||
|
$maxFiles
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated use LoggerFactory::getOxidLogPath
|
||||||
|
* @param string $fileName
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
public function getOxidLogPath(string $fileName): string
|
public function getOxidLogPath(string $fileName): string
|
||||||
{
|
{
|
||||||
if (!class_exists(Registry::class)) {
|
if (!class_exists(Registry::class)) {
|
||||||
|
@ -18,11 +18,10 @@ declare(strict_types=1);
|
|||||||
namespace D3\GuzzleFactory;
|
namespace D3\GuzzleFactory;
|
||||||
|
|
||||||
use D3\GuzzleFactory\Apps\OxidLoggerTrait;
|
use D3\GuzzleFactory\Apps\OxidLoggerTrait;
|
||||||
|
use D3\LoggerFactory\LoggerFactory;
|
||||||
use Exception;
|
use Exception;
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use Monolog\Handler\AbstractProcessingHandler;
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
|
||||||
use Monolog\Handler\StreamHandler;
|
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
|
||||||
@ -34,6 +33,11 @@ trait LoggerTrait
|
|||||||
protected array $loggers = [];
|
protected array $loggers = [];
|
||||||
protected ?int $messageLevel = null;
|
protected ?int $messageLevel = null;
|
||||||
|
|
||||||
|
protected function getLoggerFactory(): LoggerFactory
|
||||||
|
{
|
||||||
|
return LoggerFactory::create();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* @throws InvalidArgumentException
|
* @throws InvalidArgumentException
|
||||||
@ -45,14 +49,12 @@ trait LoggerTrait
|
|||||||
?int $maxFiles = null
|
?int $maxFiles = null
|
||||||
): void
|
): void
|
||||||
{
|
{
|
||||||
$logger = new Logger($loggerName);
|
$this->loggers[$loggerName] = $this->getLoggerFactory()
|
||||||
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
->getFileLogger($loggerName, $filePath, $logLevel, $maxFiles);
|
||||||
$logger->pushHandler($stream_handler);
|
|
||||||
|
|
||||||
$this->loggers[$loggerName] = $logger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @deprecated use LoggerFactory::getFileLoggerStreamHandler
|
||||||
* @param string $filePath
|
* @param string $filePath
|
||||||
* @param int $logLevel
|
* @param int $logLevel
|
||||||
* @param int|null $maxFiles
|
* @param int|null $maxFiles
|
||||||
@ -65,9 +67,7 @@ trait LoggerTrait
|
|||||||
?int $maxFiles = null
|
?int $maxFiles = null
|
||||||
): AbstractProcessingHandler
|
): AbstractProcessingHandler
|
||||||
{
|
{
|
||||||
return is_null($maxFiles) ?
|
return $this->getLoggerFactory()->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||||||
new StreamHandler($filePath, $logLevel) :
|
|
||||||
new RotatingFileHandler($filePath, $maxFiles, $logLevel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function addConfiguredLogger(LoggerInterface $logger): void
|
public function addConfiguredLogger(LoggerInterface $logger): void
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
namespace D3\GuzzleFactory\tests\Apps;
|
namespace D3\GuzzleFactory\tests\Apps;
|
||||||
|
|
||||||
use D3\GuzzleFactory\GuzzleFactory;
|
use D3\GuzzleFactory\GuzzleFactory;
|
||||||
|
use D3\LoggerFactory\LoggerFactory;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
@ -40,25 +41,6 @@ trait OxidLoggerTestTrait
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @test
|
|
||||||
* @return void
|
|
||||||
* @throws ReflectionException
|
|
||||||
* @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger
|
|
||||||
*/
|
|
||||||
public function testAddCombinedOxidAndFileLoggerWithoutOxid(): void
|
|
||||||
{
|
|
||||||
$sut = GuzzleFactory::create();
|
|
||||||
|
|
||||||
$this->expectException(RuntimeException::class);
|
|
||||||
|
|
||||||
$this->callMethod(
|
|
||||||
$sut,
|
|
||||||
'addCombinedOxidAndFileLogger',
|
|
||||||
['nameFixture', 'file/path.log', 1, 5]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @return void
|
* @return void
|
||||||
@ -109,11 +91,19 @@ trait OxidLoggerTestTrait
|
|||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger
|
* @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger
|
||||||
*/
|
*/
|
||||||
public function testAddCombinedOxidAndFileLoggerInOxid(): void
|
public function testAddCombinedOxidAndFileLogger(): void
|
||||||
{
|
{
|
||||||
require_once __DIR__.'/../Helpers/classAliases.php';
|
require_once __DIR__.'/../Helpers/classAliases.php';
|
||||||
|
|
||||||
$sut = GuzzleFactory::create();
|
$loggerFactory = $this->getMockBuilder(LoggerFactory::class)
|
||||||
|
->onlyMethods(['getCombinedOxidAndFileLogger'])
|
||||||
|
->getMock();
|
||||||
|
$loggerFactory->expects($this->once())->method('getCombinedOxidAndFileLogger');
|
||||||
|
|
||||||
|
$sut = $this->getMockBuilder(GuzzleFactory::class)
|
||||||
|
->onlyMethods(['getLoggerFactory'])
|
||||||
|
->getMock();
|
||||||
|
$sut->method('getLoggerFactory')->willReturn($loggerFactory);
|
||||||
|
|
||||||
$this->setValue($sut, 'loggers', ['oxid' => 1]);
|
$this->setValue($sut, 'loggers', ['oxid' => 1]);
|
||||||
|
|
||||||
|
@ -16,8 +16,9 @@
|
|||||||
namespace D3\GuzzleFactory\tests;
|
namespace D3\GuzzleFactory\tests;
|
||||||
|
|
||||||
use D3\GuzzleFactory\GuzzleFactory;
|
use D3\GuzzleFactory\GuzzleFactory;
|
||||||
|
use D3\LoggerFactory\LoggerFactory;
|
||||||
use Generator;
|
use Generator;
|
||||||
use Monolog\Handler\RotatingFileHandler;
|
use Monolog\Handler\AbstractProcessingHandler;
|
||||||
use Monolog\Handler\StreamHandler;
|
use Monolog\Handler\StreamHandler;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
@ -26,15 +27,45 @@ trait LoggerTestTrait
|
|||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
|
* @return void
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @covers \D3\GuzzleFactory\GuzzleFactory::addFileLogger
|
* @covers \D3\GuzzleFactory\GuzzleFactory::getLoggerFactory
|
||||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getFileLoggerStreamHandler
|
|
||||||
* @dataProvider addFileLoggerDataProvider
|
|
||||||
*/
|
*/
|
||||||
public function testAddFileLogger(int $logLevel, ?int $maxFiles, string $expectedHandlerClass): void
|
public function testGetLoggerFactory(): void
|
||||||
{
|
{
|
||||||
$sut = GuzzleFactory::create();
|
$sut = GuzzleFactory::create();
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
LoggerFactory::class,
|
||||||
|
$this->callMethod(
|
||||||
|
$sut,
|
||||||
|
'getLoggerFactory',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @covers \D3\GuzzleFactory\GuzzleFactory::addFileLogger
|
||||||
|
* @dataProvider addFileLoggerDataProvider
|
||||||
|
*/
|
||||||
|
public function testAddFileLogger(int $logLevel, ?int $maxFiles): void
|
||||||
|
{
|
||||||
|
$logger = $this->getMockBuilder(Logger::class)
|
||||||
|
->disableOriginalConstructor()
|
||||||
|
->getMock();
|
||||||
|
|
||||||
|
$loggerFactory = $this->getMockBuilder(LoggerFactory::class)
|
||||||
|
->onlyMethods(['getFileLogger'])
|
||||||
|
->getMock();
|
||||||
|
$loggerFactory->expects($this->once())->method('getFileLogger')->willReturn($logger);
|
||||||
|
|
||||||
|
$sut = $this->getMockBuilder(GuzzleFactory::class)
|
||||||
|
->onlyMethods(['getLoggerFactory'])
|
||||||
|
->getMock();
|
||||||
|
$sut->method("getLoggerFactory")->willReturn($loggerFactory);
|
||||||
|
|
||||||
$this->callMethod(
|
$this->callMethod(
|
||||||
$sut,
|
$sut,
|
||||||
'addFileLogger',
|
'addFileLogger',
|
||||||
@ -44,15 +75,11 @@ trait LoggerTestTrait
|
|||||||
$loggers = $this->getValue($sut, 'loggers');
|
$loggers = $this->getValue($sut, 'loggers');
|
||||||
$this->assertArrayHasKey('nameFixture', $loggers);
|
$this->assertArrayHasKey('nameFixture', $loggers);
|
||||||
$this->assertInstanceOf(Logger::class, $loggers['nameFixture']);
|
$this->assertInstanceOf(Logger::class, $loggers['nameFixture']);
|
||||||
$this->assertInstanceOf($expectedHandlerClass, $loggers['nameFixture']->getHandlers()[0]);
|
|
||||||
$this->assertSame($logLevel, $loggers['nameFixture']->popHandler('nameFixture')->getLevel());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function addFileLoggerDataProvider(): Generator
|
public static function addFileLoggerDataProvider(): Generator
|
||||||
{
|
{
|
||||||
yield 'no rotation' => [Logger::INFO, null, StreamHandler::class];
|
yield [Logger::INFO, null];
|
||||||
yield 'rotation 1' => [Logger::ERROR, 1, RotatingFileHandler::class];
|
|
||||||
yield 'rotation 20' => [Logger::DEBUG, 20, RotatingFileHandler::class];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user