Skip to content

Commit dd2d206

Browse files
Omer LAKRAAnicolas-grekasGromNaN
committed
Add enabled parameter in configuration
Co-authored-by: Nicolas Grekas <nicolas.grekas@gmail.com> Co-authored-by: Jérôme Tamarelle <jerome@tamarelle.net>
1 parent 58ff90b commit dd2d206

File tree

8 files changed

+70
-2
lines changed

8 files changed

+70
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* Deprecate `sentry` and `raven` handler, use a `service` handler with [`sentry/sentry-symfony`](https://docs.sentry.io/platforms/php/guides/symfony/logs/) instead
1515
* Add configuration for Gelf encoders
1616
* Fix `host` configuration for `elastic_search` handler
17+
* Add `enabled` option to `handlers` configuration
1718

1819
## 3.10.0 (2023-11-06)
1920

config/schema/monolog-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<xsd:attribute name="webhook-url" type="xsd:string" />
9494
<xsd:attribute name="slack-team" type="xsd:string" />
9595
<xsd:attribute name="region" type="xsd:string" />
96+
<xsd:attribute name="enabled" type="xsd:boolean" />
9697
</xsd:complexType>
9798

9899
<xsd:simpleType name="level">

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ public function getConfigTreeBuilder(): TreeBuilder
457457
->end()
458458
->end()
459459
->scalarNode('id')->end() // service & rollbar
460+
->booleanNode('enabled')->defaultTrue()->end()
460461
->scalarNode('priority')->defaultValue(0)->end()
461462
->scalarNode('level')->defaultValue('DEBUG')->end()
462463
->booleanNode('bubble')->defaultTrue()->end()

src/DependencyInjection/MonologExtension.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function load(array $configs, ContainerBuilder $container)
6767
$handlers = [];
6868

6969
foreach ($config['handlers'] as $name => $handler) {
70+
if (!$handler['enabled']) {
71+
continue;
72+
}
7073
$handlers[$handler['priority']][] = [
7174
'id' => $this->buildHandler($container, $name, $handler),
7275
'channels' => empty($handler['channels']) ? null : $handler['channels'],

tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
17+
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1718
use Symfony\Component\Config\FileLocator;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Definition;
@@ -232,6 +233,31 @@ private function getFunctionalContainer()
232233

233234
return $container;
234235
}
236+
237+
/**
238+
* @testWith [true]
239+
* [false]
240+
*/
241+
public function testEnabledHandler(bool $enabled)
242+
{
243+
$container = new ContainerBuilder();
244+
$loader = new MonologExtension();
245+
246+
$config = [
247+
'handlers' => [
248+
'main' => [
249+
'enabled' => $enabled,
250+
'type' => 'stream',
251+
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
252+
'level' => 'debug',
253+
],
254+
],
255+
];
256+
257+
$loader->load([$config], $container);
258+
259+
$this->assertSame($enabled, $container->hasDefinition('monolog.handler.main'));
260+
}
235261
}
236262

237263
class DummyService

tests/DependencyInjection/FixtureMonologExtensionTestCase.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public function testPsr3MessageProcessingDisabled()
253253
$this->assertNotContainsEquals(['pushProcessor', [new Reference('monolog.processor.psr_log_message')]], $methodCalls, 'The PSR-3 processor should not be enabled');
254254
}
255255

256-
public function testPsrLogMessageProcessorHasConstructorArguments(): void
256+
public function testPsrLogMessageProcessorHasConstructorArguments()
257257
{
258258
$reflectionConstructor = (new \ReflectionClass(PsrLogMessageProcessor::class))->getConstructor();
259259
if (null === $reflectionConstructor || $reflectionConstructor->getNumberOfParameters() <= 0) {
@@ -280,7 +280,7 @@ public function testPsrLogMessageProcessorHasConstructorArguments(): void
280280
}
281281
}
282282

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

326+
public function testEnabledHandleOption()
327+
{
328+
$container = $this->getContainer('enabled_handlers');
329+
330+
$this->assertTrue($container->hasDefinition('monolog.handler.default'));
331+
$this->assertTrue($container->hasDefinition('monolog.handler.enabled'));
332+
$this->assertFalse($container->hasDefinition('monolog.handler.disabled'));
333+
}
334+
326335
protected function getContainer($fixture)
327336
{
328337
$container = new ContainerBuilder();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:monolog="http://symfony.com/schema/dic/monolog"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/monolog http://symfony.com/schema/dic/monolog/monolog-1.0.xsd">
8+
9+
<monolog:config>
10+
<monolog:handler name="default" type="stream" path="/tmp/symfony.log"/>
11+
<monolog:handler name="enabled" type="stream" path="/tmp/symfony.log" enabled="true"/>
12+
<monolog:handler name="disabled" type="stream" path="/tmp/symfony.log" enabled="false"/>
13+
</monolog:config>
14+
</container>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
monolog:
2+
handlers:
3+
default:
4+
type: stream
5+
path: /tmp/symfony.log
6+
enabled:
7+
type: stream
8+
path: /tmp/symfony.log
9+
enabled: true
10+
disabled:
11+
type: stream
12+
path: /tmp/symfony.log
13+
enabled: false

0 commit comments

Comments
 (0)