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
44 changes: 44 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - mongo:
* - mongo:
* - id: optional if host is given
* - host: database host name, optional if id is given
* - [port]: defaults to 27017
* - [user]: database user name
* - pass: mandatory only if user is present
* - [database]: defaults to monolog
* - [collection]: defaults to logs
* - [level]: level name or int value, defaults to DEBUG
* - [bubble]: bool, defaults to true
*
* - fingers_crossed:
* - handler: the wrapped handler's name
* - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING
Expand Down Expand Up @@ -247,6 +259,34 @@ public function getConfigTreeBuilder()
->thenInvalid('What must be set is either the hostname or the id.')
->end()
->end() // gelf
->arrayNode('mongo')
->canBeUnset()
->beforeNormalization()
->ifString()
->then(function($v) { return array('id'=> $v); })
->end()
->children()
->scalarNode('id')->end()
->scalarNode('host')->end()
->scalarNode('port')->defaultValue(27017)->end()
->scalarNode('user')->end()
->scalarNode('pass')->end()
->scalarNode('database')->defaultValue('monolog')->end()
->scalarNode('collection')->defaultValue('logs')->end()
->end()
->validate()
->ifTrue(function($v) {
return !isset($v['id']) && !isset($v['host']);
})
->thenInvalid('What must be set is either the host or the id.')
->end()
->validate()
->ifTrue(function($v) {
return isset($v['user']) && !isset($v['pass']);
})
->thenInvalid('If you set user, you must provide a password.')
->end()
->end() // mongo
->arrayNode('members') // group
->canBeUnset()
->performNoDeepMerging()
Expand Down Expand Up @@ -440,6 +480,10 @@ public function getConfigTreeBuilder()
->ifTrue(function($v) { return 'cube' === $v['type'] && empty($v['url']); })
->thenInvalid('The url has to be specified to use a CubeHandler')
->end()
->validate()
->ifTrue(function($v) { return 'mongo' === $v['type'] && !isset($v['mongo']); })
->thenInvalid('The mongo configuration has to be specified to use a MongoHandler')
->end()
->validate()
->ifTrue(function($v) { return 'amqp' === $v['type'] && empty($v['exchange']); })
->thenInvalid('The exchange has to be specified to use a AmqpHandler')
Expand Down
31 changes: 31 additions & 0 deletions DependencyInjection/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,37 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
));
break;

case 'mongo':

if (isset($handler['mongo']['id'])) {
$clientId = $handler['mongo']['id'];
} else {
$server = 'mongodb://';

if(isset($handler['mongo']['user'])) {
$server .= $handler['mongo']['user'] . ':' . $handler['mongo']['pass'] . '@';
}

$server .= $handler['mongo']['host'] . ':' . $handler['mongo']['port'];

$client = new Definition("%monolog.mongo.client.class%", array(
$server
));

$clientId = uniqid('monolog.mongo.client.');
$client->setPublic(false);
$container->setDefinition($clientId, $client);
}

$definition->setArguments(array(
new Reference($clientId),
$handler['mongo']['database'],
$handler['mongo']['collection'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot access them without checking they are set, as you don't enforce it in the configuration class. there is 3 solutions to avoid errors:

  • add default values in the configuration
  • mark them as required in the configuration class (which would make it impossible to use your shortcut notation for id-based config as these settings would be missing)
  • throw an exception in the DI extension when they are not set (equivalent to the above case, with a dirtier code and the same limitation)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

absolutely right, I've added the default values in the configuration. I've also changed the comment in the Configuration.php file, to be more clear and to reflect this change.

$handler['level'],
$handler['bubble'],
));
break;

case 'chromephp':
$definition->setArguments(array(
$handler['level'],
Expand Down
3 changes: 3 additions & 0 deletions Resources/config/monolog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@

<parameter key="monolog.handler.fingers_crossed.class">Monolog\Handler\FingersCrossedHandler</parameter>
<parameter key="monolog.handler.fingers_crossed.error_level_activation_strategy.class">Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy</parameter>
<parameter key="monolog.handler.mongo.class">Monolog\Handler\MongoDBHandler</parameter>
<parameter key="monolog.mongo.client.class">MongoClient</parameter>

</parameters>

<services>
Expand Down
12 changes: 12 additions & 0 deletions Resources/config/schema/monolog-1.0.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<xsd:element name="member" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
<xsd:element name="channels" type="channels" minOccurs="0" maxOccurs="1" />
<xsd:element name="publisher" type="publisher" minOccurs="0" maxOccurs="1" />
<xsd:element name="mongo" type="mongo" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="priority" type="xsd:integer" />
Expand Down Expand Up @@ -89,4 +90,15 @@
<xsd:enumeration value="exclusive" />
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="mongo">
<xsd:attribute name="id" type="xsd:string" />
<xsd:attribute name="host" type="xsd:string" />
<xsd:attribute name="port" type="xsd:integer" />
<xsd:attribute name="user" type="xsd:string" />
<xsd:attribute name="pass" type="xsd:string" />
<xsd:attribute name="database" type="xsd:string" />
<xsd:attribute name="collection" type="xsd:string" />
</xsd:complexType>

</xsd:schema>