Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Deprecate `sentry` and `raven` handler, use a `service` handler with [`sentry/sentry-symfony`](https://docs.sentry.io/platforms/php/guides/symfony/logs/) instead
* Add configuration for Gelf encoders
* Fix `host` configuration for `elastic_search` handler
* Add `enabled` option to `handlers` configuration

## 3.10.0 (2023-11-06)

Expand Down
1 change: 1 addition & 0 deletions config/schema/monolog-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
<xsd:attribute name="webhook-url" type="xsd:string" />
<xsd:attribute name="slack-team" type="xsd:string" />
<xsd:attribute name="region" type="xsd:string" />
<xsd:attribute name="enabled" type="xsd:boolean" />
</xsd:complexType>

<xsd:simpleType name="level">
Expand Down
1 change: 1 addition & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('id')->end() // service & rollbar
->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('priority')->defaultValue(0)->end()
->scalarNode('level')->defaultValue('DEBUG')->end()
->booleanNode('bubble')->defaultTrue()->end()
Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function load(array $configs, ContainerBuilder $container)
$handlers = [];

foreach ($config['handlers'] as $name => $handler) {
if (!$handler['enabled']) {
continue;
}
$handlers[$handler['priority']][] = [
'id' => $this->buildHandler($container, $name, $handler),
'channels' => empty($handler['channels']) ? null : $handler['channels'],
Expand Down
26 changes: 26 additions & 0 deletions tests/DependencyInjection/Compiler/LoggerChannelPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -232,6 +233,31 @@ private function getFunctionalContainer()

return $container;
}

/**
* @testWith [true]
* [false]
*/
public function testEnabledHandler(bool $enabled)
{
$container = new ContainerBuilder();
$loader = new MonologExtension();

$config = [
'handlers' => [
'main' => [
'enabled' => $enabled,
'type' => 'stream',
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
'level' => 'debug',
],
],
];

$loader->load([$config], $container);

$this->assertSame($enabled, $container->hasDefinition('monolog.handler.main'));
}
}

class DummyService
Expand Down
13 changes: 11 additions & 2 deletions tests/DependencyInjection/FixtureMonologExtensionTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public function testPsr3MessageProcessingDisabled()
$this->assertNotContainsEquals(['pushProcessor', [new Reference('monolog.processor.psr_log_message')]], $methodCalls, 'The PSR-3 processor should not be enabled');
}

public function testPsrLogMessageProcessorHasConstructorArguments(): void
public function testPsrLogMessageProcessorHasConstructorArguments()
{
$reflectionConstructor = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
if (null === $reflectionConstructor || $reflectionConstructor->getNumberOfParameters() <= 0) {
Expand All @@ -280,7 +280,7 @@ public function testPsrLogMessageProcessorHasConstructorArguments(): void
}
}

public function testPsrLogMessageProcessorDoesNotHaveConstructorArguments(): void
public function testPsrLogMessageProcessorDoesNotHaveConstructorArguments()
{
$reflectionConstructor = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
if (null !== $reflectionConstructor && $reflectionConstructor->getNumberOfParameters() > 0) {
Expand Down Expand Up @@ -323,6 +323,15 @@ public function testTypeNull()
$this->assertSame('DEBUG', $logger->getArgument(0));
}

public function testEnabledHandleOption()
{
$container = $this->getContainer('enabled_handlers');

$this->assertTrue($container->hasDefinition('monolog.handler.default'));
$this->assertTrue($container->hasDefinition('monolog.handler.enabled'));
$this->assertFalse($container->hasDefinition('monolog.handler.disabled'));
}

protected function getContainer($fixture)
{
$container = new ContainerBuilder();
Expand Down
14 changes: 14 additions & 0 deletions tests/DependencyInjection/Fixtures/xml/enabled_handlers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:monolog="http://symfony.com/schema/dic/monolog"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">

<monolog:config>
<monolog:handler name="default" type="stream" path="/tmp/symfony.log"/>
<monolog:handler name="enabled" type="stream" path="/tmp/symfony.log" enabled="true"/>
<monolog:handler name="disabled" type="stream" path="/tmp/symfony.log" enabled="false"/>
</monolog:config>
</container>
13 changes: 13 additions & 0 deletions tests/DependencyInjection/Fixtures/yml/enabled_handlers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
monolog:
handlers:
default:
type: stream
path: /tmp/symfony.log
enabled:
type: stream
path: /tmp/symfony.log
enabled: true
disabled:
type: stream
path: /tmp/symfony.log
enabled: false