Skip to content

Commit ec54eb1

Browse files
committed
feature #550 Deprecate "mongo" handler type in favor of new "mongodb" syntax (jmikola, GromNaN)
This PR was merged into the 3.x branch. Discussion ---------- Deprecate "mongo" handler type in favor of new "mongodb" syntax | Q | A | ------------- | --- | Branch? | 3.x <!-- for features and bug --> | Bug fix? | no | New feature? | yes | Deprecations? | yes | Issues | [PHPORM-398](https://jira.mongodb.org/browse/PHPORM-398) | License | MIT Define a new "mongodb" handler type. It accepts an "id" reference like the old "mongo" type; however, "uri" instead of a single "host" and "port". The "uri" option is more flexible. Additionally, the "username" and "password" options have been renamed and are no longer used to modify the connection string directly ("mongo" never applied URL encoding). Instead, the options are set in the URI options array, which does not require encoding. The "mongodb" never requires a password, as a username alone is valid for some auth mechanisms. Lastly, a "monolog-bundle" app name is specified when the bundle constructs a MongoDB\Client instance for both "mongo" and "mongodb" handler syntax. Commits ------- c1899dd Deprecate "mongo" handler type in favor of new "mongodb" syntax
2 parents 4fb8f83 + c1899dd commit ec54eb1

File tree

9 files changed

+315
-4
lines changed

9 files changed

+315
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
php: [ '8.1', '8.2', '8.3', '8.4' ]
1515
monolog: [ '2.*' ]
1616
symfony: [ false ]
17+
extensions: [ '' ]
1718
include:
1819
- php: '8.1'
1920
deps: lowest
@@ -26,6 +27,8 @@ jobs:
2627
- php: '8.4'
2728
deps: highest
2829
monolog: '3.*'
30+
- php: '8.4'
31+
extensions: mongodb
2932

3033
env:
3134
SYMFONY_REQUIRE: ${{ matrix.symfony }}
@@ -40,6 +43,7 @@ jobs:
4043
php-version: ${{ matrix.php }}
4144
ini-values: zend.exception_ignore_args=false
4245
tools: flex
46+
extensions: ${{ matrix.extensions }}
4347

4448
- name: Configure composer
4549
if: "${{ matrix.deps == 'highest' }}"
@@ -49,6 +53,10 @@ jobs:
4953
if: "${{ matrix.monolog != '' }}"
5054
run: composer require --no-update monolog/monolog:${{ matrix.monolog }}
5155

56+
- name: Require mongodb/mongodb if ext-mongodb is available
57+
if: "${{ contains(matrix.extensions, 'mongodb') }}"
58+
run: composer require --no-update mongodb/mongodb
59+
5260
- name: Composer install
5361
uses: ramsey/composer-install@v3
5462
with:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Add `hosts` configuration for `elastica` handler
1818
* Add `enabled` option to `handlers` configuration
1919
* Add `priority` field to `processor` tag
20+
* Add `mongodb` handler and deprecate `mongo`
2021

2122
## 3.10.0 (2023-11-06)
2223

config/schema/monolog-1.0.xsd

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
2222
<xsd:element name="publisher" type="publisher" minOccurs="0" maxOccurs="1" />
2323
<xsd:element name="mongo" type="mongo" minOccurs="0" maxOccurs="1" />
24+
<xsd:element name="mongodb" type="mongodb" minOccurs="0" maxOccurs="1" />
2425
<xsd:element name="elasticsearch" type="elasticsearch" minOccurs="0" maxOccurs="1" />
2526
<xsd:element name="config" type="xsd:anyType" minOccurs="0" maxOccurs="1" />
2627
<xsd:element name="excluded-404" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -163,6 +164,15 @@
163164
<xsd:attribute name="collection" type="xsd:string" />
164165
</xsd:complexType>
165166

167+
<xsd:complexType name="mongodb">
168+
<xsd:attribute name="id" type="xsd:string" />
169+
<xsd:attribute name="uri" type="xsd:string" />
170+
<xsd:attribute name="username" type="xsd:string" />
171+
<xsd:attribute name="password" type="xsd:string" />
172+
<xsd:attribute name="database" type="xsd:string" />
173+
<xsd:attribute name="collection" type="xsd:string" />
174+
</xsd:complexType>
175+
166176
<xsd:complexType name="redis">
167177
<xsd:attribute name="id" type="xsd:string" />
168178
<xsd:attribute name="host" type="xsd:string" />

src/DependencyInjection/Configuration.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@
9090
* - [level]: level name or int value, defaults to DEBUG
9191
* - [bubble]: bool, defaults to true
9292
*
93+
* - mongodb:
94+
* - mongodb:
95+
* - id: optional if uri is given
96+
* - uri: MongoDB connection string, optional if id is given
97+
* - [username]: Username for database authentication
98+
* - [password]: Password for database authentication
99+
* - [database]: Database to which logs are written (not used for auth), defaults to "monolog"
100+
* - [collection]: Collection to which logs are written, defaults to "logs"
101+
* - [level]: level name or int value, defaults to DEBUG
102+
* - [bubble]: bool, defaults to true
103+
*
93104
* - elastic_search:
94105
* - elasticsearch:
95106
* - id: optional if host is given
@@ -648,6 +659,7 @@ public function getConfigTreeBuilder(): TreeBuilder
648659

649660
$this->addGelfSection($handlerNode);
650661
$this->addMongoSection($handlerNode);
662+
$this->addMongoDBSection($handlerNode);
651663
$this->addElasticsearchSection($handlerNode);
652664
$this->addRedisSection($handlerNode);
653665
$this->addPredisSection($handlerNode);
@@ -879,7 +891,7 @@ private function addMongoSection(ArrayNodeDefinition $handlerNode)
879891
->ifTrue(function ($v) {
880892
return !isset($v['id']) && !isset($v['host']);
881893
})
882-
->thenInvalid('What must be set is either the host or the id.')
894+
->thenInvalid('The "mongo" handler configuration requires either a service "id" or a connection "host".')
883895
->end()
884896
->validate()
885897
->ifTrue(function ($v) {
@@ -891,7 +903,43 @@ private function addMongoSection(ArrayNodeDefinition $handlerNode)
891903
->end()
892904
->validate()
893905
->ifTrue(function ($v) { return 'mongo' === $v['type'] && !isset($v['mongo']); })
894-
->thenInvalid('The mongo configuration has to be specified to use a MongoHandler')
906+
->thenInvalid('The "mongo" configuration has to be specified to use a "mongo" handler type.')
907+
->end()
908+
;
909+
}
910+
911+
private function addMongoDBSection(ArrayNodeDefinition $handlerNode)
912+
{
913+
$handlerNode
914+
->children()
915+
->arrayNode('mongodb')
916+
->canBeUnset()
917+
->beforeNormalization()
918+
->ifString()
919+
->then(function ($v) { return ['id' => $v]; })
920+
->end()
921+
->children()
922+
->scalarNode('id')
923+
->info('ID of a MongoDB\Client service')
924+
->example('doctrine_mongodb.odm.logs_connection')
925+
->end()
926+
->scalarNode('uri')->end()
927+
->scalarNode('username')->end()
928+
->scalarNode('password')->end()
929+
->scalarNode('database')->defaultValue('monolog')->end()
930+
->scalarNode('collection')->defaultValue('logs')->end()
931+
->end()
932+
->validate()
933+
->ifTrue(function ($v) {
934+
return !isset($v['id']) && !isset($v['uri']);
935+
})
936+
->thenInvalid('The "mongodb" handler configuration requires either a service "id" or a connection "uri".')
937+
->end()
938+
->end()
939+
->end()
940+
->validate()
941+
->ifTrue(function ($v) { return 'mongodb' === $v['type'] && !isset($v['mongodb']); })
942+
->thenInvalid('The "mongodb" configuration has to be specified to use a "mongodb" handler type.')
895943
->end()
896944
;
897945
}

src/DependencyInjection/MonologExtension.php

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
265265
break;
266266

267267
case 'mongo':
268+
trigger_deprecation('symfony/monolog-bundle', '3.11', 'The "mongo" handler type is deprecated in MonologBundle since version 3.11.0, use the "mongodb" type instead.');
269+
270+
if (!class_exists('MongoDB\Client')) {
271+
throw new \RuntimeException('The "mongo" handler requires the mongodb/mongodb package to be installed.');
272+
}
273+
268274
if (isset($handler['mongo']['id'])) {
269275
$client = new Reference($handler['mongo']['id']);
270276
} else {
@@ -278,9 +284,8 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
278284

279285
$client = new Definition('MongoDB\Client', [
280286
$server,
287+
['appname' => 'monolog-bundle'],
281288
]);
282-
283-
$client->setPublic(false);
284289
}
285290

286291
$definition->setArguments([
@@ -292,6 +297,44 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
292297
]);
293298
break;
294299

300+
case 'mongodb':
301+
if (!class_exists('MongoDB\Client')) {
302+
throw new \RuntimeException('The "mongodb" handler requires the mongodb/mongodb package to be installed.');
303+
}
304+
305+
if (isset($handler['mongodb']['id'])) {
306+
$client = new Reference($handler['mongodb']['id']);
307+
} else {
308+
$uriOptions = ['appname' => 'monolog-bundle'];
309+
310+
if (isset($handler['mongodb']['username'])) {
311+
$uriOptions['username'] = $handler['mongodb']['username'];
312+
}
313+
314+
if (isset($handler['mongodb']['password'])) {
315+
$uriOptions['password'] = $handler['mongodb']['password'];
316+
}
317+
318+
$client = new Definition('MongoDB\Client', [
319+
$handler['mongodb']['uri'],
320+
$uriOptions,
321+
]);
322+
}
323+
324+
$definition->setArguments([
325+
$client,
326+
$handler['mongodb']['database'],
327+
$handler['mongodb']['collection'],
328+
$handler['level'],
329+
$handler['bubble'],
330+
]);
331+
332+
if (empty($handler['formatter'])) {
333+
$formatter = new Definition('Monolog\Formatter\MongoDBFormatter');
334+
$definition->addMethodCall('setFormatter', [$formatter]);
335+
}
336+
break;
337+
295338
case 'elasticsearch':
296339
trigger_deprecation('symfony/monolog-bundle', '3.8', 'The "elasticsearch" handler type is deprecated in MonologBundle since version 3.8.0, use the "elastica" type instead, or switch to the official Elastic client using the "elastic_search" type.');
297340
// no break
@@ -1021,6 +1064,7 @@ private function getHandlerClassByType($handlerType)
10211064
'fingers_crossed' => 'Monolog\Handler\FingersCrossedHandler',
10221065
'filter' => 'Monolog\Handler\FilterHandler',
10231066
'mongo' => 'Monolog\Handler\MongoDBHandler',
1067+
'mongodb' => 'Monolog\Handler\MongoDBHandler',
10241068
'elasticsearch' => 'Monolog\Handler\ElasticSearchHandler',
10251069
'telegram' => 'Monolog\Handler\TelegramBotHandler',
10261070
'server_log' => 'Symfony\Bridge\Monolog\Handler\ServerLogHandler',

tests/DependencyInjection/FixtureMonologExtensionTestCase.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\MonologBundle\Tests\DependencyInjection;
1313

1414
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
15+
use Monolog\Handler\MongoDBHandler;
1516
use Monolog\Handler\NoopHandler;
1617
use Monolog\Handler\NullHandler;
1718
use Monolog\Processor\PsrLogMessageProcessor;
@@ -332,6 +333,23 @@ public function testEnabledHandleOption()
332333
$this->assertFalse($container->hasDefinition('monolog.handler.disabled'));
333334
}
334335

336+
public function testMongoDB()
337+
{
338+
if (!class_exists('MongoDB\Client')) {
339+
$this->markTestSkipped('mongodb/mongodb is not installed.');
340+
}
341+
342+
$container = $this->getContainer('mongodb');
343+
344+
$this->assertTrue($container->hasDefinition('monolog.handler.mongodb'));
345+
$handler = $container->getDefinition('monolog.handler.mongodb');
346+
$this->assertDICDefinitionClass($handler, MongoDBHandler::class);
347+
$client = $handler->getArgument(0);
348+
$this->assertDICDefinitionClass($client, 'MongoDB\Client');
349+
$this->assertDICConstructorArguments($client, ['mongodb://localhost:27018', ['appname' => 'monolog-bundle', 'username' => 'username', 'password' => 'password']]);
350+
$this->assertDICConstructorArguments($handler, [$client, 'db', 'coll', 'DEBUG', true]);
351+
}
352+
335353
protected function getContainer($fixture)
336354
{
337355
$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+
<srv:container xmlns="http://symfony.com/schema/dic/monolog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:srv="http://symfony.com/schema/dic/services"
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+
<config>
10+
<handler name="mongodb" type="mongodb">
11+
<mongodb uri="mongodb://localhost:27018" username="username" password="password" database="db" collection="coll" />
12+
</handler>
13+
</config>
14+
</srv:container>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
monolog:
2+
handlers:
3+
mongodb:
4+
type: mongodb
5+
mongodb:
6+
uri: "mongodb://localhost:27018"
7+
username: username
8+
password: password
9+
database: db
10+
collection: coll

0 commit comments

Comments
 (0)