Skip to content

Commit fcd3680

Browse files
committed
Merge branch '3.x' into 4.x
* 3.x: Add enabled parameter in configuration
2 parents b9d3b3a + 26ecfa2 commit fcd3680

File tree

8 files changed

+67
-0
lines changed

8 files changed

+67
-0
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
@@ -87,6 +87,7 @@
8787
<xsd:attribute name="content-type" type="xsd:string" />
8888
<xsd:attribute name="webhook-url" type="xsd:string" />
8989
<xsd:attribute name="region" type="xsd:string" />
90+
<xsd:attribute name="enabled" type="xsd:boolean" />
9091
</xsd:complexType>
9192

9293
<xsd:simpleType name="level">

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ public function getConfigTreeBuilder(): TreeBuilder
409409
->end()
410410
->end()
411411
->scalarNode('id')->end() // service & rollbar
412+
->booleanNode('enabled')->defaultTrue()->end()
412413
->scalarNode('priority')->defaultValue(0)->end()
413414
->scalarNode('level')->defaultValue('DEBUG')->end()
414415
->booleanNode('bubble')->defaultTrue()->end()

src/DependencyInjection/MonologExtension.php

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

6262
foreach ($config['handlers'] as $name => $handler) {
63+
if (!$handler['enabled']) {
64+
continue;
65+
}
6366
$handlers[$handler['priority']][] = [
6467
'id' => $this->buildHandler($container, $name, $handler),
6568
'channels' => empty($handler['channels']) ? null : $handler['channels'],

tests/DependencyInjection/Compiler/LoggerChannelPassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection\Compiler;
1313

14+
use PHPUnit\Framework\Attributes\TestWith;
1415
use PHPUnit\Framework\TestCase;
1516
use Psr\Log\LoggerInterface;
1617
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
18+
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1719
use Symfony\Component\Config\FileLocator;
1820
use Symfony\Component\DependencyInjection\ContainerBuilder;
1921
use Symfony\Component\DependencyInjection\Definition;
@@ -229,6 +231,29 @@ private function getFunctionalContainer(): ContainerBuilder
229231

230232
return $container;
231233
}
234+
235+
#[TestWith([true])]
236+
#[TestWith([false])]
237+
public function testEnabledHandler(bool $enabled)
238+
{
239+
$container = new ContainerBuilder();
240+
$loader = new MonologExtension();
241+
242+
$config = [
243+
'handlers' => [
244+
'main' => [
245+
'enabled' => $enabled,
246+
'type' => 'stream',
247+
'path' => '%kernel.logs_dir%/%kernel.environment%.log',
248+
'level' => 'debug',
249+
],
250+
],
251+
];
252+
253+
$loader->load([$config], $container);
254+
255+
$this->assertSame($enabled, $container->hasDefinition('monolog.handler.main'));
256+
}
232257
}
233258

234259
class DummyService

tests/DependencyInjection/FixtureMonologExtensionTestCase.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,15 @@ public function testTypeNull()
254254
$this->assertSame('DEBUG', $logger->getArgument(0));
255255
}
256256

257+
public function testEnabledHandleOption()
258+
{
259+
$container = $this->getContainer('enabled_handlers');
260+
261+
$this->assertTrue($container->hasDefinition('monolog.handler.default'));
262+
$this->assertTrue($container->hasDefinition('monolog.handler.enabled'));
263+
$this->assertFalse($container->hasDefinition('monolog.handler.disabled'));
264+
}
265+
257266
protected function getContainer($fixture): ContainerBuilder
258267
{
259268
$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)