use LoggerFactory
This commit is contained in:
parent
fa142578d8
commit
b69b3d206c
@ -20,7 +20,8 @@
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^7.0",
|
||||
"monolog/monolog": "^1.20",
|
||||
"d3/sensitive-message-formatter": "^1.0"
|
||||
"d3/sensitive-message-formatter": "^1.0",
|
||||
"d3/logger-factory": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.5",
|
||||
|
@ -17,9 +17,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace D3\GuzzleFactory\Apps;
|
||||
|
||||
use D3\LoggerFactory\LoggerFactory;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use OxidEsales\Eshop\Core\Registry;
|
||||
use RuntimeException;
|
||||
@ -46,25 +46,23 @@ trait OxidLoggerTrait
|
||||
?int $maxFiles = null
|
||||
): 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'])) {
|
||||
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
|
||||
{
|
||||
if (!class_exists(Registry::class)) {
|
||||
|
@ -18,11 +18,10 @@ declare(strict_types=1);
|
||||
namespace D3\GuzzleFactory;
|
||||
|
||||
use D3\GuzzleFactory\Apps\OxidLoggerTrait;
|
||||
use D3\LoggerFactory\LoggerFactory;
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
@ -34,6 +33,11 @@ trait LoggerTrait
|
||||
protected array $loggers = [];
|
||||
protected ?int $messageLevel = null;
|
||||
|
||||
protected function getLoggerFactory(): LoggerFactory
|
||||
{
|
||||
return LoggerFactory::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* @throws InvalidArgumentException
|
||||
@ -45,14 +49,12 @@ trait LoggerTrait
|
||||
?int $maxFiles = null
|
||||
): void
|
||||
{
|
||||
$logger = new Logger($loggerName);
|
||||
$stream_handler = $this->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||||
$logger->pushHandler($stream_handler);
|
||||
|
||||
$this->loggers[$loggerName] = $logger;
|
||||
$this->loggers[$loggerName] = $this->getLoggerFactory()
|
||||
->getFileLogger($loggerName, $filePath, $logLevel, $maxFiles);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use LoggerFactory::getFileLoggerStreamHandler
|
||||
* @param string $filePath
|
||||
* @param int $logLevel
|
||||
* @param int|null $maxFiles
|
||||
@ -65,9 +67,7 @@ trait LoggerTrait
|
||||
?int $maxFiles = null
|
||||
): AbstractProcessingHandler
|
||||
{
|
||||
return is_null($maxFiles) ?
|
||||
new StreamHandler($filePath, $logLevel) :
|
||||
new RotatingFileHandler($filePath, $maxFiles, $logLevel);
|
||||
return $this->getLoggerFactory()->getFileLoggerStreamHandler($filePath, $logLevel, $maxFiles);
|
||||
}
|
||||
|
||||
public function addConfiguredLogger(LoggerInterface $logger): void
|
||||
|
@ -16,6 +16,7 @@
|
||||
namespace D3\GuzzleFactory\tests\Apps;
|
||||
|
||||
use D3\GuzzleFactory\GuzzleFactory;
|
||||
use D3\LoggerFactory\LoggerFactory;
|
||||
use Monolog\Logger;
|
||||
use ReflectionException;
|
||||
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
|
||||
* @return void
|
||||
@ -109,11 +91,19 @@ trait OxidLoggerTestTrait
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::addCombinedOxidAndFileLogger
|
||||
*/
|
||||
public function testAddCombinedOxidAndFileLoggerInOxid(): void
|
||||
public function testAddCombinedOxidAndFileLogger(): void
|
||||
{
|
||||
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]);
|
||||
|
||||
|
@ -16,8 +16,9 @@
|
||||
namespace D3\GuzzleFactory\tests;
|
||||
|
||||
use D3\GuzzleFactory\GuzzleFactory;
|
||||
use D3\LoggerFactory\LoggerFactory;
|
||||
use Generator;
|
||||
use Monolog\Handler\RotatingFileHandler;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Handler\StreamHandler;
|
||||
use Monolog\Logger;
|
||||
use ReflectionException;
|
||||
@ -26,15 +27,45 @@ trait LoggerTestTrait
|
||||
{
|
||||
/**
|
||||
* @test
|
||||
* @return void
|
||||
* @throws ReflectionException
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::addFileLogger
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getFileLoggerStreamHandler
|
||||
* @dataProvider addFileLoggerDataProvider
|
||||
* @covers \D3\GuzzleFactory\GuzzleFactory::getLoggerFactory
|
||||
*/
|
||||
public function testAddFileLogger(int $logLevel, ?int $maxFiles, string $expectedHandlerClass): void
|
||||
public function testGetLoggerFactory(): void
|
||||
{
|
||||
$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(
|
||||
$sut,
|
||||
'addFileLogger',
|
||||
@ -44,15 +75,11 @@ trait LoggerTestTrait
|
||||
$loggers = $this->getValue($sut, 'loggers');
|
||||
$this->assertArrayHasKey('nameFixture', $loggers);
|
||||
$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
|
||||
{
|
||||
yield 'no rotation' => [Logger::INFO, null, StreamHandler::class];
|
||||
yield 'rotation 1' => [Logger::ERROR, 1, RotatingFileHandler::class];
|
||||
yield 'rotation 20' => [Logger::DEBUG, 20, RotatingFileHandler::class];
|
||||
yield [Logger::INFO, null];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user