Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Commit

Permalink
forward time to Monolog (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmi20 authored Sep 2, 2021
1 parent 8e513da commit 1ba532e
Show file tree
Hide file tree
Showing 7 changed files with 417 additions and 49 deletions.
10 changes: 5 additions & 5 deletions src/AddFormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ trait AddFormatterTrait
*/
private function addFormatter(ContainerInterface $container, HandlerInterface $handler, ?array $options = null): void
{
if (!$handler instanceof FormattableHandlerInterface) {
return;
}

if (!is_array($options) || !array_key_exists('formatter', $options)) {
if (
!$handler instanceof FormattableHandlerInterface
|| !is_array($options)
|| !array_key_exists('formatter', $options)
) {
return;
}

Expand Down
10 changes: 5 additions & 5 deletions src/AddProcessorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ trait AddProcessorTrait
*/
private function addProcessor(ContainerInterface $container, HandlerInterface $handler, ?array $options = null): void
{
if (!$handler instanceof ProcessableHandlerInterface) {
return;
}

if (!is_array($options) || !array_key_exists('processors', $options)) {
if (
!$handler instanceof ProcessableHandlerInterface
|| !is_array($options)
|| !array_key_exists('processors', $options)
) {
return;
}

Expand Down
16 changes: 10 additions & 6 deletions src/Handler/FingersCrossedHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Monolog\Handler\FingersCrossedHandler;
use Monolog\Logger;
use Psr\Container\ContainerExceptionInterface;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LogLevel;

use function array_key_exists;
Expand Down Expand Up @@ -157,18 +158,21 @@ private function getActivationStrategy(ContainerInterface $container, $activatio
}
}

if (is_string($activationStrategy)) {
if (!$activationStrategyPluginManager->has($activationStrategy)) {
throw new ServiceNotCreatedException('Could not find Class for ActivationStrategy');
}

if (is_string($activationStrategy) && $activationStrategyPluginManager->has($activationStrategy)) {
try {
return $activationStrategyPluginManager->get($activationStrategy);
} catch (ServiceNotFoundException | ServiceNotCreatedException $e) {
throw new ServiceNotFoundException('Could not load ActivationStrategy class', 0, $e);
}
}

return $activationStrategy;
try {
/* @phpstan-ignore-next-line */
return Logger::toMonologLevel($activationStrategy);
} catch (InvalidArgumentException $e) {
// do nothing here
}

throw new ServiceNotCreatedException('Could not find Class for ActivationStrategy');
}
}
20 changes: 15 additions & 5 deletions src/LoggerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Mimmi20\LoggerFactory;

use DateTimeZone;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Laminas\Log\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -150,14 +151,23 @@ public function __invoke(ContainerInterface $container, $requestedName, ?array $
}

if (isset($logConfig['name']) && array_key_exists('handlers', $logConfig) && is_array($logConfig['handlers'])) {
$monologConfig = [
'name' => $logConfig['name'],
'handlers' => $logConfig['handlers'],
'processors' => $logConfig['monolog_processors'] ?? [],
];

if (
array_key_exists('timezone', $logConfig)
&& (is_string($logConfig['timezone']) || $logConfig['timezone'] instanceof DateTimeZone)
) {
$monologConfig['timezone'] = $logConfig['timezone'];
}

try {
$monolog = $container->get(MonologPluginManager::class)->get(
\Monolog\Logger::class,
[
'name' => $logConfig['name'],
'handlers' => $logConfig['handlers'],
'processors' => $logConfig['monolog_processors'] ?? [],
]
$monologConfig
);
} catch (ContainerExceptionInterface $e) {
throw new ServiceNotCreatedException(sprintf('Could not find service %s', MonologPluginManager::class), 0, $e);
Expand Down
107 changes: 101 additions & 6 deletions tests/Handler/FingersCrossedHandlerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ public function testInvoceWithHandlerConfig4(): void

/**
* @throws Exception
* @throws ReflectionException
*/
public function testInvoceWithHandlerConfig5(): void
{
Expand All @@ -593,8 +594,9 @@ public function testInvoceWithHandlerConfig5(): void
->getMock();
$handler2->expects(self::never())
->method('setFormatter');
$handler2->expects(self::never())
->method('getFormatter');
$handler2->expects(self::once())
->method('getFormatter')
->willReturn($formatterClass);

$monologHandlerPluginManager = $this->getMockBuilder(AbstractPluginManager::class)
->disableOriginalConstructor()
Expand All @@ -618,11 +620,49 @@ public function testInvoceWithHandlerConfig5(): void

$factory = new FingersCrossedHandlerFactory();

$this->expectException(ServiceNotCreatedException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Could not find Class for ActivationStrategy');
$handler = $factory($container, '', ['handler' => ['type' => $type, 'enabled' => true], 'activationStrategy' => $strategy, 'bufferSize' => 42, 'bubble' => false, 'stopBuffering' => false, 'passthruLevel' => LogLevel::WARNING]);

$factory($container, '', ['handler' => ['type' => $type, 'enabled' => true], 'activationStrategy' => $strategy, 'bufferSize' => 42, 'bubble' => false, 'stopBuffering' => false, 'passthruLevel' => LogLevel::WARNING]);
self::assertInstanceOf(FingersCrossedHandler::class, $handler);

$handlerP = new ReflectionProperty($handler, 'handler');
$handlerP->setAccessible(true);

self::assertSame($handler2, $handlerP->getValue($handler));

$as = new ReflectionProperty($handler, 'activationStrategy');
$as->setAccessible(true);

self::assertInstanceOf(ErrorLevelActivationStrategy::class, $as->getValue($handler));

$bs = new ReflectionProperty($handler, 'bufferSize');
$bs->setAccessible(true);

self::assertSame(42, $bs->getValue($handler));

$b = new ReflectionProperty($handler, 'bubble');
$b->setAccessible(true);

self::assertFalse($b->getValue($handler));

$sb = new ReflectionProperty($handler, 'stopBuffering');
$sb->setAccessible(true);

self::assertFalse($sb->getValue($handler));

$ptl = new ReflectionProperty($handler, 'passthruLevel');
$ptl->setAccessible(true);

self::assertSame(Logger::WARNING, $ptl->getValue($handler));

self::assertSame($formatterClass, $handler->getFormatter());

$proc = new ReflectionProperty($handler, 'processors');
$proc->setAccessible(true);

$processors = $proc->getValue($handler);

self::assertIsArray($processors);
self::assertCount(0, $processors);
}

/**
Expand Down Expand Up @@ -1058,6 +1098,61 @@ public function testInvoceWithHandlerConfig11(): void
$factory($container, '', ['handler' => ['type' => $type, 'enabled' => true], 'activationStrategy' => [], 'bufferSize' => 42, 'bubble' => false, 'stopBuffering' => false, 'passthruLevel' => LogLevel::WARNING]);
}

/**
* @throws Exception
*/
public function testInvoceWithHandlerConfig12(): void
{
$type = 'abc';
$strategy = 'xyz';

$activationStrategyPluginManager = $this->getMockBuilder(AbstractPluginManager::class)
->disableOriginalConstructor()
->getMock();
$activationStrategyPluginManager->expects(self::once())
->method('has')
->with($strategy)
->willReturn(false);
$activationStrategyPluginManager->expects(self::never())
->method('get');

$handler2 = $this->getMockBuilder(ChromePHPHandler::class)
->disableOriginalConstructor()
->getMock();
$handler2->expects(self::never())
->method('setFormatter');
$handler2->expects(self::never())
->method('getFormatter');

$monologHandlerPluginManager = $this->getMockBuilder(AbstractPluginManager::class)
->disableOriginalConstructor()
->getMock();
$monologHandlerPluginManager->expects(self::never())
->method('has');
$monologHandlerPluginManager->expects(self::once())
->method('get')
->with($type, [])
->willReturn($handler2);

$container = $this->getMockBuilder(ContainerInterface::class)
->disableOriginalConstructor()
->getMock();
$container->expects(self::never())
->method('has');
$container->expects(self::exactly(2))
->method('get')
->withConsecutive([MonologHandlerPluginManager::class], [ActivationStrategyPluginManager::class])
->willReturnOnConsecutiveCalls($monologHandlerPluginManager, $activationStrategyPluginManager);

$factory = new FingersCrossedHandlerFactory();

$this->expectException(ServiceNotCreatedException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('Could not find Class for ActivationStrategy');

$factory($container, '', ['handler' => ['type' => $type, 'enabled' => true], 'activationStrategy' => $strategy, 'bufferSize' => 42, 'bubble' => false, 'stopBuffering' => false, 'passthruLevel' => LogLevel::WARNING]);
}

/**
* @throws Exception
*/
Expand Down
Loading

0 comments on commit 1ba532e

Please sign in to comment.