Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability to configure the default channel #675

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
- `Spiral\Snapshots\SnapshotterInterface` usage replaced with `Spiral\Exceptions\ExceptionReporterInterface` in all classes.
- **Other Features**
- [spiral/debug] Added `Spiral\Debug\StateConsumerInterface`.

- [spiral/monolog-bridge] Added the ability to configure the default channel using the configuration file or
environment variable `MONOLOG_DEFAULT_CHANNEL`.

## v2.12.0 - 2022-04-07
- **Medium Impact Changes**
- Bootloaders `Spiral\Bootloader\Broadcast\BroadcastBootloader`, `Spiral\Bootloader\Http\WebsocketsBootloader`
Expand Down
4 changes: 3 additions & 1 deletion src/Bridge/Monolog/src/Bootloader/MonologBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Monolog\ResettableInterface;
use Psr\Log\LoggerInterface;
use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\Patch\Append;
Expand All @@ -35,7 +36,7 @@ public function __construct(
) {
}

public function boot(Container $container, FinalizerInterface $finalizer): void
public function boot(Container $container, FinalizerInterface $finalizer, EnvironmentInterface $env): void
{
$finalizer->addFinalizer(static function () use ($container): void {
if ($container->hasInstance(LoggerInterface::class)) {
Expand All @@ -56,6 +57,7 @@ public function boot(Container $container, FinalizerInterface $finalizer): void
});

$this->config->setDefaults(MonologConfig::CONFIG, [
'default' => $env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL),
'globalLevel' => Logger::DEBUG,
'handlers' => [],
]);
Expand Down
7 changes: 7 additions & 0 deletions src/Bridge/Monolog/src/Config/MonologConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@
final class MonologConfig extends InjectableConfig
{
public const CONFIG = 'monolog';
public const DEFAULT_CHANNEL = 'default';

/** @var array */
protected $config = [
'default' => self::DEFAULT_CHANNEL,
'globalLevel' => Logger::DEBUG,
'handlers' => [],
];

public function getDefault(): string
{
return $this->config['default'] ?? self::DEFAULT_CHANNEL;
}

public function getEventLevel(): int
{
return $this->config['globalLevel'] ?? Logger::DEBUG;
Expand Down
13 changes: 6 additions & 7 deletions src/Bridge/Monolog/src/LogFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@

final class LogFactory implements LogsInterface, InjectorInterface, ResettableInterface
{
// Default logger channel (supplied via injection)
public const DEFAULT = 'default';

private ?LoggerInterface $default = null;
private readonly HandlerInterface $eventHandler;

Expand All @@ -36,16 +33,18 @@ public function __construct(

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

if ($channel === null || $channel === $default) {
if ($this->default !== null) {
// we should use only one default logger per system
return $this->default;
}

return $this->default = new Logger(
self::DEFAULT,
$this->getHandlers(self::DEFAULT),
$this->getProcessors(self::DEFAULT)
$default,
$this->getHandlers($default),
$this->getProcessors($default)
);
}

Expand Down
21 changes: 21 additions & 0 deletions src/Bridge/Monolog/tests/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Monolog;

use PHPUnit\Framework\TestCase;
use Spiral\Boot\Environment;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Core\Container;

abstract class BaseTest extends TestCase
{
protected Container $container;

protected function setUp(): void
{
$this->container = new Container();
$this->container->bind(EnvironmentInterface::class, new Environment());
}
}
44 changes: 26 additions & 18 deletions src/Bridge/Monolog/tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;
use Monolog\ResettableInterface;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Spiral\Boot\BootloadManager;
use Spiral\Boot\Finalizer;
Expand All @@ -31,27 +30,37 @@
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\LogFactory;

class FactoryTest extends TestCase
class FactoryTest extends BaseTest
{
use MockeryPHPUnitIntegration;

public function testDefaultLogger(): void
{
$factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(), new Container());
$factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(),$this->container);
$logger = $factory->getLogger();

$this->assertNotEmpty($logger);
$this->assertSame($logger, $factory->getLogger());
$this->assertSame($logger, $factory->getLogger(MonologConfig::DEFAULT_CHANNEL));
}

public function testChangedDefaultLogger(): void
{
$factory = new LogFactory(new MonologConfig(['default' => 'foo']), new ListenerRegistry(), $this->container);

$logger = $factory->getLogger();

$this->assertNotEmpty($logger);
$this->assertSame($logger, $factory->getLogger());
$this->assertSame($logger, $factory->getLogger(LogFactory::DEFAULT));
$this->assertSame($logger, $factory->getLogger('foo'));
}

public function testInjection(): void
{
$factory = new LogFactory(new MonologConfig([]), new ListenerRegistry(), new Container());
$logger = $factory->getLogger();

$container = new Container();
$container->bind(ConfiguratorInterface::class, new ConfigManager(
$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
new class() implements LoaderInterface {
public function has(string $section): bool
{
Expand All @@ -65,20 +74,19 @@ public function load(string $section): array
}
));

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

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

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

public function testFinalizerShouldResetDefaultLogger()
{
$container = new Container();
$container->bind(ConfiguratorInterface::class, new ConfigManager(
$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
new class() implements LoaderInterface {
public function has(string $section): bool
{
Expand All @@ -92,7 +100,7 @@ public function load(string $section): array
}
));

$container->bind(FinalizerInterface::class, $finalizer = new Finalizer());
$this->container->bind(FinalizerInterface::class, $finalizer = new Finalizer());

$factory = new LogFactory(new MonologConfig([
'handlers' => [
Expand All @@ -105,14 +113,14 @@ public function load(string $section): array
$processor = \Mockery::mock(ProcessorInterface::class, ResettableInterface::class)
]
]
]), new ListenerRegistry(), $container);
]), new ListenerRegistry(), $this->container);

$handler->shouldReceive('reset')->once();
$processor->shouldReceive('reset')->once();

$container->bind(LogFactory::class, $factory);
$container->get(BootloadManager::class)->bootload([MonologBootloader::class]);
$container->get(LogsInterface::class)->getLogger();
$this->container->bind(LogFactory::class, $factory);
$this->container->get(BootloadManager::class)->bootload([MonologBootloader::class]);
$this->container->get(LogsInterface::class)->getLogger();
$finalizer->finalize();
}
}
8 changes: 2 additions & 6 deletions src/Bridge/Monolog/tests/HandlersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Monolog\Handler\NullHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Spiral\Boot\BootloadManager;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfigManager;
Expand All @@ -27,14 +26,11 @@
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\Exception\ConfigException;

class HandlersTest extends TestCase
class HandlersTest extends BaseTest
{
/** @var Container */
private $container;

public function setUp(): void
{
$this->container = new Container();
parent::setUp();

$this->container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
$finalizer->shouldReceive('addFinalizer')->once();
Expand Down
14 changes: 6 additions & 8 deletions src/Bridge/Monolog/tests/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Spiral\Boot\BootloadManager;
use Spiral\Boot\Finalizer;
Expand All @@ -19,14 +18,13 @@
use Spiral\Monolog\Bootloader\MonologBootloader;
use Spiral\Monolog\LogFactory;

class LoggerTest extends TestCase
class LoggerTest extends BaseTest
{
use MockeryPHPUnitIntegration;

public function testLoggerShouldBeReset()
{
$container = new Container();
$container->bind(ConfiguratorInterface::class, new ConfigManager(
$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
new class() implements LoaderInterface {
public function has(string $section): bool
{
Expand All @@ -40,16 +38,16 @@ public function load(string $section): array
}
));

$container->bind(FinalizerInterface::class, $finalizer = new Finalizer());
$container->bind(LogFactory::class, $injector = m::mock(Container\InjectorInterface::class));
$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);

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

$finalizer->finalize();
}
Expand Down
10 changes: 2 additions & 8 deletions src/Bridge/Monolog/tests/ProcessorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,23 @@
namespace Spiral\Tests\Monolog;

use Monolog\Processor\PsrLogMessageProcessor;
use PHPUnit\Framework\TestCase;
use Spiral\Boot\BootloadManager;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfigManager;
use Spiral\Config\ConfiguratorInterface;
use Spiral\Config\LoaderInterface;
use Spiral\Core\Container;
use Spiral\Logger\ListenerRegistry;
use Spiral\Logger\ListenerRegistryInterface;
use Spiral\Logger\LogsInterface;
use Spiral\Monolog\Bootloader\MonologBootloader;
use Spiral\Monolog\Config\MonologConfig;
use Spiral\Monolog\Exception\ConfigException;

class ProcessorsTest extends TestCase
class ProcessorsTest extends BaseTest
{
/** @var Container */
private $container;

public function setUp(): void
{
$this->container = new Container();
parent::setUp();

$this->container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
$finalizer->shouldReceive('addFinalizer')->once();
Expand Down Expand Up @@ -147,7 +142,6 @@ public function testConstructWithOptionsProcessor(): void
$this->assertInstanceOf(PsrLogMessageProcessor::class, $processor);

$property = new \ReflectionProperty(PsrLogMessageProcessor::class, 'dateFormat');
$property->setAccessible(true);

$this->assertSame('c', $property->getValue($processor));
}
Expand Down
13 changes: 5 additions & 8 deletions src/Bridge/Monolog/tests/RotateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Spiral\Boot\BootloadManager;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfigManager;
Expand All @@ -22,16 +21,14 @@
use Spiral\Core\Container;
use Spiral\Monolog\Bootloader\MonologBootloader;

class RotateHandlerTest extends TestCase
class RotateHandlerTest extends BaseTest
{
public function testRotateHandler(): void
{
$container = new Container();

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

$container->bind(ConfiguratorInterface::class, new ConfigManager(
$this->container->bind(ConfiguratorInterface::class, new ConfigManager(
new class() implements LoaderInterface {
public function has(string $section): bool
{
Expand All @@ -44,14 +41,14 @@ public function load(string $section): array
}
}
));
$container->get(BootloadManager::class)->bootload([MonologBootloader::class]);
$this->container->get(BootloadManager::class)->bootload([MonologBootloader::class]);

$autowire = new Container\Autowire('log.rotate', [
'filename' => 'monolog.log'
]);

/** @var RotatingFileHandler $handler */
$handler = $autowire->resolve($container);
$handler = $autowire->resolve($this->container);
$this->assertInstanceOf(RotatingFileHandler::class, $handler);

$this->assertSame(Logger::DEBUG, $handler->getLevel());
Expand Down
Loading