Skip to content

Commit

Permalink
Log compiler pass message only when one or more channels were excluded
Browse files Browse the repository at this point in the history
  • Loading branch information
HypeMC committed Feb 20, 2023
1 parent 3770889 commit cfb79e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
12 changes: 7 additions & 5 deletions src/DependencyInjection/Compiler/ExcludeMonologChannelPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public function process(ContainerBuilder $container): void
}
$container->setParameter('monolog.handlers_to_channels', $handlersToChannels);

$container->log($this, sprintf(
'Excluded Monolog channel "%s" from the following exclusive handlers "%s".',
$monologChannelName,
implode('", "', $exclusiveHandlerNames)
));
if ($exclusiveHandlerNames) {
$container->log($this, sprintf(
'Excluded Monolog channel "%s" from the following exclusive handlers "%s".',
$monologChannelName,
implode('", "', $exclusiveHandlerNames)
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ final class ExcludeMonologChannelPassTest extends TestCase
/**
* @dataProvider handlerChannels
*/
public function testChannelIsExcludedWhenExpected(string $type, array $elements, array $expectedElements): void
public function testChannelIsExcludedWhenExpected(?array $channels, array $expectedChannels, array $expectedLog): void
{
$container = new ContainerBuilder();
$container->setParameter('bizkit_loggable_command.channel_name', 'channel_name');
$container->setParameter('monolog.handlers_to_channels', [
'foobar' => ['type' => $type, 'elements' => $elements],
]);
$container->setParameter('monolog.handlers_to_channels', ['monolog.handler.foobar' => $channels]);

(new ExcludeMonologChannelPass())->process($container);

/** @var array<string, array{type: string, elements: list<string>}> $actualElements */
$actualElements = $container->getParameter('monolog.handlers_to_channels');
$this->assertSame($expectedElements, $actualElements['foobar']['elements']);
/** @var array<string, array{type: string, elements: list<string>}> $handlersToChannels */
$handlersToChannels = $container->getParameter('monolog.handlers_to_channels');
$this->assertSame($expectedChannels, $handlersToChannels['monolog.handler.foobar']['elements']);

$this->assertSame($expectedLog, $container->getCompiler()->getLog());
}

public function handlerChannels(): iterable
{
yield 'Inclusive' => ['inclusive', ['foo', 'bar', 'baz'], ['foo', 'bar', 'baz']];
yield 'Exclusive without exception' => ['exclusive', ['foo', 'baz'], ['foo', 'baz', 'channel_name']];
yield 'Exclusive with exception' => ['exclusive', ['foo', '!channel_name', 'baz'], ['foo', 'baz']];
$log = sprintf('%s: Excluded Monolog channel "channel_name" from the following exclusive handlers "foobar".', ExcludeMonologChannelPass::class);

yield 'Inclusive' => [['type' => 'inclusive', 'elements' => ['foo', 'bar', 'baz']], ['foo', 'bar', 'baz'], []];
yield 'Exclusive without exception' => [['type' => 'exclusive', 'elements' => ['foo', 'baz']], ['foo', 'baz', 'channel_name'], [$log]];
yield 'Exclusive with exception' => [['type' => 'exclusive', 'elements' => ['foo', '!channel_name', 'baz']], ['foo', 'baz'], []];
}
}

0 comments on commit cfb79e9

Please sign in to comment.