Skip to content

Commit

Permalink
Add Factory for RotatingFileHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkorotkiewicz committed Feb 23, 2022
1 parent 15f4cd5 commit a84e014
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ConfigProvider.php
Expand Up @@ -5,12 +5,14 @@
namespace SmartFrame\Logger;

use Monolog\Handler\ElasticsearchHandler;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\SocketHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use SmartFrame\Logger\HandlerFactory\ElasticsearchHandlerFactory;
use SmartFrame\Logger\HandlerFactory\RotatingFileHandlerFactory;
use SmartFrame\Logger\HandlerFactory\SocketHandlerFactory;
use SmartFrame\Logger\HandlerFactory\StreamHandlerFactory;
use SmartFrame\Logger\Initializer\LoggerAwareInitializer;
Expand All @@ -36,7 +38,8 @@ public function getDependencies(): array
'factories' => [
ElasticsearchHandler::class => ElasticsearchHandlerFactory::class,
StreamHandler::class => StreamHandlerFactory::class,
SocketHandler::class => SocketHandlerFactory::class
SocketHandler::class => SocketHandlerFactory::class,
RotatingFileHandler::class => RotatingFileHandlerFactory::class,
],
'invokables' => [
NullLogger::class,
Expand Down
32 changes: 32 additions & 0 deletions src/HandlerFactory/RotatingFileHandlerFactory.php
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SmartFrame\Logger\HandlerFactory;


use Interop\Container\ContainerInterface;
use Monolog\Handler\HandlerInterface;
use Monolog\Handler\RotatingFileHandler;

class RotatingFileHandlerFactory extends StreamHandlerFactory
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null): HandlerInterface
{
if (!isset($options['stream'])) {
throw new MissingOptionsException('Missing "stream" option');
}

$handler = new RotatingFileHandler($options['stream'], $options['maxFiles'] ?? 0);

if (isset($options['formatter'])) {
$handler = $this->applyFormatters($handler, $options['formatter']);
}

if (isset($options['properties'])) {
$this->applyProperties($handler, $options['properties']);
}

return $this->applySpecialHandlers($handler, $options);
}
}
23 changes: 23 additions & 0 deletions test/HandlerFactory/RotatingFileHandlerFactoryTest.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace SmartFrameTest\Logger\HandlerFactory;

use Interop\Container\ContainerInterface;
use Monolog\Handler\RotatingFileHandler;
use PHPUnit\Framework\TestCase;
use SmartFrame\Logger\HandlerFactory\RotatingFileHandlerFactory;

class RotatingFileHandlerFactoryTest extends TestCase
{

public function testInvoke(): void
{
$containerMock = $this->createMock(ContainerInterface::class);

$instance = (new RotatingFileHandlerFactory())->__invoke($containerMock, RotatingFileHandler::class, ['stream' => 'data/logs/test.log', 'maxFiles' => 2]);

self::assertInstanceOf(RotatingFileHandler::class, $instance);
}
}

0 comments on commit a84e014

Please sign in to comment.