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

[spiral/monolog-bridge] Adding the ability to configure the Monolog messages format #994

Merged
merged 5 commits into from
Sep 14, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- **Other Features**
- [spiral/queue] Added `Spiral\Queue\Interceptor\Consume\RetryPolicyInterceptor` to enable automatic job retries
with a configurable retry policy.
- [spiral/monolog-bridge] Added the ability to configure the **Monolog** messages format via environment variable
`MONOLOG_FORMAT`.

## 3.8.4 - 2023-09-08

Expand Down
11 changes: 7 additions & 4 deletions src/Bridge/Monolog/src/Bootloader/MonologBootloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ final class MonologBootloader extends Bootloader implements Container\SingletonI
'log.rotate' => [self::class, 'logRotate'],
];

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

public function __construct(
private readonly ConfiguratorInterface $config
private readonly ConfiguratorInterface $config,
private readonly EnvironmentInterface $env,
) {
}

public function init(Container $container, FinalizerInterface $finalizer, EnvironmentInterface $env): void
public function init(Container $container, FinalizerInterface $finalizer): void
{
$finalizer->addFinalizer(static function (bool $terminate) use ($container): void {
if ($terminate) {
Expand All @@ -61,7 +64,7 @@ public function init(Container $container, FinalizerInterface $finalizer, Enviro
});

$this->config->setDefaults(MonologConfig::CONFIG, [
'default' => $env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL),
'default' => $this->env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL),
'globalLevel' => Logger::DEBUG,
'handlers' => [],
]);
Expand Down Expand Up @@ -101,7 +104,7 @@ public function logRotate(
);

return $handler->setFormatter(
new LineFormatter("[%datetime%] %level_name%: %message% %context%\n")
new LineFormatter($this->env->get('MONOLOG_FORMAT', self::DEFAULT_FORMAT))
);
}
}
68 changes: 68 additions & 0 deletions src/Bridge/Monolog/tests/Config/MonologConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Monolog\Config;

use Monolog\Handler\HandlerInterface;
use Monolog\Logger;
use Monolog\Processor\ProcessorInterface;
use PHPUnit\Framework\TestCase;
use Spiral\Monolog\Config\MonologConfig;

final class MonologConfigTest extends TestCase
{
public function testGetDefault(): void
{
$config = new MonologConfig();
$this->assertSame(MonologConfig::DEFAULT_CHANNEL, $config->getDefault());

$config = new MonologConfig(['default' => 'foo']);
$this->assertSame('foo', $config->getDefault());
}

public function testGetEventLevel(): void
{
$config = new MonologConfig();
$this->assertSame(Logger::DEBUG, $config->getEventLevel());

$config = new MonologConfig(['globalLevel' => Logger::INFO]);
$this->assertSame(Logger::INFO, $config->getEventLevel());
}

public function testGetHandlers(): void
{
$config = new MonologConfig();
$this->assertEmpty(\iterator_to_array($config->getHandlers('foo')));

$config = new MonologConfig([
'handlers' => [
'foo' => [
$this->createMock(HandlerInterface::class)
]
]
]);
$this->assertInstanceOf(
HandlerInterface::class,
\iterator_to_array($config->getHandlers('foo'))[0]
);
}

public function testGetProcessors(): void
{
$config = new MonologConfig();
$this->assertEmpty(\iterator_to_array($config->getProcessors('foo')));

$config = new MonologConfig([
'processors' => [
'foo' => [
$this->createMock(ProcessorInterface::class)
]
]
]);
$this->assertInstanceOf(
ProcessorInterface::class,
\iterator_to_array($config->getProcessors('foo'))[0]
);
}
}
37 changes: 37 additions & 0 deletions src/Bridge/Monolog/tests/RotateHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Spiral\Boot\BootloadManager\DefaultInvokerStrategy;
use Spiral\Boot\BootloadManager\Initializer;
use Spiral\Boot\BootloadManager\InitializerInterface;
use Spiral\Boot\BootloadManager\InvokerStrategyInterface;
use Spiral\Boot\BootloadManager\StrategyBasedBootloadManager;
use Spiral\Boot\Environment;
use Spiral\Boot\EnvironmentInterface;
use Spiral\Boot\FinalizerInterface;
use Spiral\Config\ConfigManager;
use Spiral\Config\ConfiguratorInterface;
Expand All @@ -16,6 +22,14 @@

class RotateHandlerTest extends BaseTest
{
protected function setUp(): void
{
parent::setUp();

$this->container->bind(InvokerStrategyInterface::class, DefaultInvokerStrategy::class);
$this->container->bind(InitializerInterface::class, Initializer::class);
}

public function testRotateHandler(): void
{
$this->container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
Expand Down Expand Up @@ -46,4 +60,27 @@ public function load(string $section): array

$this->assertSame(Logger::DEBUG, $handler->getLevel());
}

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

$this->container->bind(EnvironmentInterface::class, new Environment(['MONOLOG_FORMAT' => 'foo']));
$this->container->bind(ConfiguratorInterface::class, $this->createMock(ConfiguratorInterface::class));
$this->container->get(StrategyBasedBootloadManager::class)->bootload([MonologBootloader::class]);

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

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

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

$formatter = $handler->getFormatter();
$this->assertSame('foo', (new \ReflectionProperty($formatter, 'format'))->getValue($formatter));
}
}