Skip to content

Commit

Permalink
Merge pull request #1102: Expose LoggerChannel attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
spiralbot committed Apr 25, 2024
1 parent b647b45 commit 037cd1e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
23 changes: 11 additions & 12 deletions phpunit.xml
@@ -1,28 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
stderr="true">
stderr="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="memory_limit" value="-1"/>
</php>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
9 changes: 6 additions & 3 deletions src/Bootloader/MonologBootloader.php
Expand Up @@ -18,6 +18,7 @@
use Spiral\Config\Patch\Append;
use Spiral\Core\Attribute\Singleton;
use Spiral\Core\Container;
use Spiral\Logger\Bootloader\LoggerBootloader;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\LogFactory;
Expand All @@ -27,13 +28,17 @@ final class MonologBootloader extends Bootloader
{
protected const SINGLETONS = [
LogsInterface::class => LogFactory::class,
LoggerInterface::class => Logger::class,
Logger::class => Logger::class,
];

protected const BINDINGS = [
'log.rotate' => [self::class, 'logRotate'],
];

protected const DEPENDENCIES = [
LoggerBootloader::class,
];

private const DEFAULT_FORMAT = "[%datetime%] %level_name%: %message% %context%\n";

public function __construct(
Expand Down Expand Up @@ -71,8 +76,6 @@ public function init(Container $container, FinalizerInterface $finalizer): void
'globalLevel' => Logger::DEBUG,
'handlers' => [],
]);

$container->bindInjector(Logger::class, LogFactory::class);
}

public function addHandler(string $channel, HandlerInterface $handler): void
Expand Down
9 changes: 6 additions & 3 deletions src/LogFactory.php
Expand Up @@ -14,6 +14,7 @@
use Spiral\Core\Container\InjectorInterface;
use Spiral\Core\FactoryInterface;
use Spiral\Logger\ListenerRegistryInterface;
use Spiral\Logger\LoggerInjector;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\Exception\ConfigException;
Expand All @@ -34,7 +35,7 @@ public function __construct(
$this->eventHandler = new EventHandler($listenerRegistry, $config->getEventLevel());
}

public function getLogger(string $channel = null): LoggerInterface
public function getLogger(?string $channel = null): LoggerInterface
{
$default = $this->config->getDefault();

Expand All @@ -58,9 +59,11 @@ public function getLogger(string $channel = null): LoggerInterface
);
}

public function createInjection(\ReflectionClass $class, string $context = null): LoggerInterface
/**
* @deprecated use {@see LoggerInjector} as an injector instead.
*/
public function createInjection(\ReflectionClass $class, ?string $context = null): LoggerInterface
{
// always return default logger as injection
return $this->getLogger();
}

Expand Down
31 changes: 31 additions & 0 deletions tests/FactoryTest.php
Expand Up @@ -17,6 +17,7 @@
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\LoaderInterface;
use Spiral\Core\Container;
use Spiral\Logger\Attribute\LoggerChannel;
use Spiral\Logger\ListenerRegistry;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Bootloader\MonologBootloader;
Expand Down Expand Up @@ -74,9 +75,39 @@ public function load(string $section): array
$this->container->bind(LogFactory::class, $factory);

$this->assertSame($logger, $this->container->get(Logger::class));
$this->assertInstanceOf(LoggerInterface::class, $this->container->get(LoggerInterface::class));
$this->assertSame($logger, $this->container->get(LoggerInterface::class));
}

public function testInjectionWithAttribute(): void
{
$factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(), new Container());

$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
new class() implements LoaderInterface {
public function has(string $section): bool
{
return false;
}

public function load(string $section): array
{
return [];
}
}
));

$this->container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
$finalizer->shouldReceive('addFinalizer')->once();

$this->container->get(StrategyBasedBootloadManager::class)->bootload([MonologBootloader::class]);
$this->container->bind(LogFactory::class, $factory);

$this->container->invoke(function (#[LoggerChannel('foo')] LoggerInterface $logger) {
$this->assertSame('foo', $logger->getName());
});
}

public function testFinalizerShouldResetDefaultLogger()
{
$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
Expand Down
8 changes: 4 additions & 4 deletions tests/LoggerTest.php
Expand Up @@ -15,6 +15,7 @@
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\LoaderInterface;
use Spiral\Core\Container;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Bootloader\MonologBootloader;
use Spiral\Monolog\LogFactory;

Expand All @@ -39,15 +40,14 @@ public function load(string $section): array
));

$this->container->bind(FinalizerInterface::class, $finalizer = new Finalizer());
$this->container->bind(LogFactory::class, $injector = m::mock(Container\InjectorInterface::class));

$logger = m::mock(Logger::class);
$logger->shouldReceive('reset')->once();

$injector->shouldReceive('createInjection')->once()->andReturn($logger);

$this->container->get(StrategyBasedBootloadManager::class)->bootload([MonologBootloader::class]);
$this->container->get(LoggerInterface::class);

$this->container->bind(LogsInterface::class, $factory = m::mock(LogsInterface::class));
$factory->shouldReceive('getLogger')->once()->andReturn($logger);

$finalizer->finalize();
}
Expand Down

0 comments on commit 037cd1e

Please sign in to comment.