Skip to content

Commit a073d14

Browse files
committed
Apply MongoDBFormatter to each "mongodb" handler by default
1 parent 41c9a49 commit a073d14

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/DependencyInjection/MonologExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,12 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
324324
$handler['level'],
325325
$handler['bubble'],
326326
]);
327+
328+
if (empty($handler['formatter'])) {
329+
$formatter = new Definition('Monolog\Formatter\MongoDBFormatter');
330+
$formatter->setPublic(false);
331+
$definition->addMethodCall('setFormatter', [$formatter]);
332+
}
327333
break;
328334

329335
case 'elasticsearch':

tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,15 +1048,21 @@ public function testMongoDB()
10481048
$this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_uri'));
10491049
$this->assertTrue($container->hasDefinition('monolog.handler.mongodb_with_uri_and_default_args'));
10501050

1051+
// A MongoDBFormatter will be applied to each handler by default
1052+
$formatter = new Definition('Monolog\Formatter\MongoDBFormatter');
1053+
$formatter->setPublic(false);
1054+
10511055
// MongoDB handler should receive the mongodb.client as first argument
10521056
$handler = $container->getDefinition('monolog.handler.mongodb_with_id');
10531057
$this->assertDICDefinitionClass($handler, MongoDBHandler::class);
10541058
$this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]);
1059+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setFormatter', [$formatter]);
10551060

10561061
// MongoDB handler should receive the mongodb.client as first argument
10571062
$handler = $container->getDefinition('monolog.handler.mongodb_with_string_id');
10581063
$this->assertDICDefinitionClass($handler, MongoDBHandler::class);
10591064
$this->assertDICConstructorArguments($handler, [new Reference('mongodb.client'), 'monolog', 'logs', 'DEBUG', true]);
1065+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setFormatter', [$formatter]);
10601066

10611067
// MongoDB handler with arguments
10621068
$handler = $container->getDefinition('monolog.handler.mongodb_with_uri');
@@ -1065,6 +1071,7 @@ public function testMongoDB()
10651071
$this->assertDICDefinitionClass($client, 'MongoDB\Client');
10661072
$this->assertDICConstructorArguments($client, ['mongodb://localhost:27018', ['appname' => 'monolog-bundle', 'username' => 'username', 'password' => 'password']]);
10671073
$this->assertDICConstructorArguments($handler, [$client, 'db', 'coll', 'DEBUG', true]);
1074+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setFormatter', [$formatter]);
10681075

10691076
// MongoDB handler with host and default arguments
10701077
$handler = $container->getDefinition('monolog.handler.mongodb_with_uri_and_default_args');
@@ -1073,6 +1080,7 @@ public function testMongoDB()
10731080
$this->assertDICDefinitionClass($client, 'MongoDB\Client');
10741081
$this->assertDICConstructorArguments($client, ['mongodb://localhost:27018', ['appname' => 'monolog-bundle']]);
10751082
$this->assertDICConstructorArguments($handler, [$client, 'monolog', 'logs', 'DEBUG', true]);
1083+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setFormatter', [$formatter]);
10761084
}
10771085

10781086
protected function getContainer(array $config = [], array $thirdPartyDefinitions = []): ContainerBuilder

0 commit comments

Comments
 (0)