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 @@
'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

Check warning on line 42 in src/Bridge/Monolog/src/Bootloader/MonologBootloader.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/src/Bootloader/MonologBootloader.php#L42

Added line #L42 was not covered by tests
{
$finalizer->addFinalizer(static function (bool $terminate) use ($container): void {
if ($terminate) {
Expand All @@ -61,7 +64,7 @@
});

$this->config->setDefaults(MonologConfig::CONFIG, [
'default' => $env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL),
'default' => $this->env->get('MONOLOG_DEFAULT_CHANNEL', MonologConfig::DEFAULT_CHANNEL),

Check warning on line 67 in src/Bridge/Monolog/src/Bootloader/MonologBootloader.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/src/Bootloader/MonologBootloader.php#L67

Added line #L67 was not covered by tests
'globalLevel' => Logger::DEBUG,
'handlers' => [],
]);
Expand Down Expand Up @@ -101,7 +104,7 @@
);

return $handler->setFormatter(
new LineFormatter("[%datetime%] %level_name%: %message% %context%\n")
new LineFormatter($this->env->get('MONOLOG_FORMAT', self::DEFAULT_FORMAT))

Check warning on line 107 in src/Bridge/Monolog/src/Bootloader/MonologBootloader.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/src/Bootloader/MonologBootloader.php#L107

Added line #L107 was not covered by tests
);
}
}
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

Check warning on line 15 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L15

Added line #L15 was not covered by tests
{
$config = new MonologConfig();
$this->assertSame(MonologConfig::DEFAULT_CHANNEL, $config->getDefault());

Check warning on line 18 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L17-L18

Added lines #L17 - L18 were not covered by tests

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

Check warning on line 21 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L20-L21

Added lines #L20 - L21 were not covered by tests
}

public function testGetEventLevel(): void

Check warning on line 24 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L24

Added line #L24 was not covered by tests
{
$config = new MonologConfig();
$this->assertSame(Logger::DEBUG, $config->getEventLevel());

Check warning on line 27 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L26-L27

Added lines #L26 - L27 were not covered by tests

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

Check warning on line 30 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L29-L30

Added lines #L29 - L30 were not covered by tests
}

public function testGetHandlers(): void

Check warning on line 33 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L33

Added line #L33 was not covered by tests
{
$config = new MonologConfig();
$this->assertEmpty(\iterator_to_array($config->getHandlers('foo')));

Check warning on line 36 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L35-L36

Added lines #L35 - L36 were not covered by tests

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

Check warning on line 48 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L38-L48

Added lines #L38 - L48 were not covered by tests
}

public function testGetProcessors(): void

Check warning on line 51 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L51

Added line #L51 was not covered by tests
{
$config = new MonologConfig();
$this->assertEmpty(\iterator_to_array($config->getProcessors('foo')));

Check warning on line 54 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L53-L54

Added lines #L53 - L54 were not covered by tests

$config = new MonologConfig([
'processors' => [
'foo' => [
$this->createMock(ProcessorInterface::class)
]
]
]);
$this->assertInstanceOf(
ProcessorInterface::class,
\iterator_to_array($config->getProcessors('foo'))[0]
);

Check warning on line 66 in src/Bridge/Monolog/tests/Config/MonologConfigTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/Config/MonologConfigTest.php#L56-L66

Added lines #L56 - L66 were not covered by tests
}
}
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

Check warning on line 25 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L25

Added line #L25 was not covered by tests
{
parent::setUp();

Check warning on line 27 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L27

Added line #L27 was not covered by tests

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

Check warning on line 30 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L29-L30

Added lines #L29 - L30 were not covered by tests
}

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

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

public function testChangeFormat(): void

Check warning on line 64 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L64

Added line #L64 was not covered by tests
{
$this->container->bind(FinalizerInterface::class, $finalizer = \Mockery::mock(FinalizerInterface::class));
$finalizer->shouldReceive('addFinalizer')->once();

Check warning on line 67 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L66-L67

Added lines #L66 - L67 were not covered by tests

$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]);

Check warning on line 71 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L69-L71

Added lines #L69 - L71 were not covered by tests

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

Check warning on line 75 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L73-L75

Added lines #L73 - L75 were not covered by tests

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

Check warning on line 79 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L78-L79

Added lines #L78 - L79 were not covered by tests

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

Check warning on line 81 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L81

Added line #L81 was not covered by tests

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

Check warning on line 84 in src/Bridge/Monolog/tests/RotateHandlerTest.php

View check run for this annotation

Codecov / codecov/patch

src/Bridge/Monolog/tests/RotateHandlerTest.php#L83-L84

Added lines #L83 - L84 were not covered by tests
}
}
Loading