Skip to content

Commit 61a78a6

Browse files
simshaunfabpot
authored andcommitted
Add configuration for new HttpCodeActivationStrategy
1 parent 8781649 commit 61a78a6

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

DependencyInjection/Configuration.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
* - handler: the wrapped handler's name
9090
* - [action_level|activation_strategy]: minimum level or service id to activate the handler, defaults to WARNING
9191
* - [excluded_404s]: if set, the strategy will be changed to one that excludes 404s coming from URLs matching any of those patterns
92+
* - [excluded_http_codes]: if set, the strategy will be changed to one that excludes specific HTTP codes
9293
* - [buffer_size]: defaults to 0 (unlimited)
9394
* - [stop_buffering]: bool to disable buffering once the handler has been activated, defaults to true
9495
* - [passthru_level]: level name or int value for messages to always flush, disabled by default
@@ -316,6 +317,7 @@ public function getConfigTreeBuilder()
316317
->prototype('array')
317318
->fixXmlConfig('member')
318319
->fixXmlConfig('excluded_404')
320+
->fixXmlConfig('excluded_http_code')
319321
->fixXmlConfig('tag')
320322
->fixXmlConfig('accepted_level')
321323
->canBeUnset()
@@ -363,6 +365,42 @@ public function getConfigTreeBuilder()
363365
->canBeUnset()
364366
->prototype('scalar')->end()
365367
->end()
368+
->arrayNode('excluded_http_codes') // fingers_crossed
369+
->canBeUnset()
370+
->beforeNormalization()
371+
->always(function ($values) {
372+
return array_map(function ($value) {
373+
/*
374+
* Allows YAML:
375+
* excluded_http_codes: [403, 404, { 400: ['^/foo', '^/bar'] }]
376+
*
377+
* and XML:
378+
* <monolog:excluded-http-code code="403">
379+
* <monolog:url>^/foo</monolog:url>
380+
* <monolog:url>^/bar</monolog:url>
381+
* </monolog:excluded-http-code>
382+
* <monolog:excluded-http-code code="404" />
383+
*/
384+
385+
if (is_array($value)) {
386+
return isset($value['code'])
387+
? $value
388+
: array('code' => key($value), 'url' => current($value));
389+
}
390+
391+
return array('code' => $value, 'url' => array());
392+
}, $values);
393+
})
394+
->end()
395+
->prototype('array')
396+
->children()
397+
->scalarNode('code')->end()
398+
->arrayNode('url')
399+
->prototype('scalar')->end()
400+
->end()
401+
->end()
402+
->end()
403+
->end()
366404
->arrayNode('accepted_levels') // filter
367405
->canBeUnset()
368406
->prototype('scalar')->end()
@@ -658,6 +696,14 @@ public function getConfigTreeBuilder()
658696
->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_404s']) && !empty($v['activation_strategy']); })
659697
->thenInvalid('You can not use excluded_404s together with a custom activation_strategy in a FingersCrossedHandler')
660698
->end()
699+
->validate()
700+
->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_http_codes']) && !empty($v['activation_strategy']); })
701+
->thenInvalid('You can not use excluded_http_codes together with a custom activation_strategy in a FingersCrossedHandler')
702+
->end()
703+
->validate()
704+
->ifTrue(function ($v) { return 'fingers_crossed' === $v['type'] && !empty($v['excluded_http_codes']) && !empty($v['excluded_404s']); })
705+
->thenInvalid('You can not use excluded_http_codes together with excluded_404s in a FingersCrossedHandler')
706+
->end()
661707
->validate()
662708
->ifTrue(function ($v) { return 'filter' === $v['type'] && "DEBUG" !== $v['min_level'] && !empty($v['accepted_levels']); })
663709
->thenInvalid('You can not use min_level together with accepted_levels in a FilterHandler')

DependencyInjection/MonologExtension.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,14 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
324324
));
325325
$container->setDefinition($handlerId.'.not_found_strategy', $activationDef);
326326
$activation = new Reference($handlerId.'.not_found_strategy');
327+
} elseif (!empty($handler['excluded_http_codes'])) {
328+
$activationDef = new Definition('Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy', array(
329+
new Reference('request_stack'),
330+
$handler['excluded_http_codes'],
331+
$handler['action_level']
332+
));
333+
$container->setDefinition($handlerId.'.http_code_strategy', $activationDef);
334+
$activation = new Reference($handlerId.'.http_code_strategy');
327335
} else {
328336
$activation = $handler['action_level'];
329337
}

Resources/config/schema/monolog-1.0.xsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<xsd:element name="elasticsearch" type="elasticsearch" minOccurs="0" maxOccurs="1" />
2525
<xsd:element name="config" type="xsd:anyType" minOccurs="0" maxOccurs="1" />
2626
<xsd:element name="excluded-404" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
27+
<xsd:element name="excluded-http-code" type="excluded-http-code" minOccurs="0" maxOccurs="unbounded" />
2728
<xsd:element name="tag" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
2829
<xsd:element name="accepted-level" type="level" minOccurs="0" maxOccurs="unbounded" />
2930
<xsd:element name="to-email" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
@@ -163,4 +164,11 @@
163164
<xsd:attribute name="user" type="xsd:string" />
164165
<xsd:attribute name="password" type="xsd:string" />
165166
</xsd:complexType>
167+
168+
<xsd:complexType name="excluded-http-code">
169+
<xsd:choice minOccurs="0" maxOccurs="unbounded">
170+
<xsd:element name="url" type="xsd:string" />
171+
</xsd:choice>
172+
<xsd:attribute name="code" type="xsd:integer" />
173+
</xsd:complexType>
166174
</xsd:schema>

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,43 @@ public function testFingersCrossedHandlerWhenExcluded404sAreSpecified()
383383
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), new Reference('monolog.handler.main.not_found_strategy'), 0, true, true, null));
384384
}
385385

386+
public function testFingersCrossedHandlerWhenExcludedHttpCodesAreSpecified()
387+
{
388+
$container = $this->getContainer(array(array('handlers' => array(
389+
'main' => array(
390+
'type' => 'fingers_crossed',
391+
'handler' => 'nested',
392+
'excluded_http_codes' => array(403, 404, array(405 => array('^/foo', '^/bar')))
393+
),
394+
'nested' => array('type' => 'stream', 'path' => '/tmp/symfony.log')
395+
))));
396+
397+
$this->assertTrue($container->hasDefinition('monolog.logger'));
398+
$this->assertTrue($container->hasDefinition('monolog.handler.main'));
399+
$this->assertTrue($container->hasDefinition('monolog.handler.nested'));
400+
$this->assertTrue($container->hasDefinition('monolog.handler.main.http_code_strategy'));
401+
402+
$logger = $container->getDefinition('monolog.logger');
403+
$this->assertDICDefinitionMethodCallAt(0, $logger, 'useMicrosecondTimestamps', array('%monolog.use_microseconds%'));
404+
$this->assertDICDefinitionMethodCallAt(1, $logger, 'pushHandler', array(new Reference('monolog.handler.main')));
405+
406+
$strategy = $container->getDefinition('monolog.handler.main.http_code_strategy');
407+
$this->assertDICDefinitionClass($strategy, 'Symfony\Bridge\Monolog\Handler\FingersCrossed\HttpCodeActivationStrategy');
408+
$this->assertDICConstructorArguments($strategy, array(
409+
new Reference('request_stack'),
410+
array(
411+
array('code' => 403, 'url' => array()),
412+
array('code' => 404, 'url' => array()),
413+
array('code' => 405, 'url' => array('^/foo', '^/bar'))
414+
),
415+
\Monolog\Logger::WARNING
416+
));
417+
418+
$handler = $container->getDefinition('monolog.handler.main');
419+
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\FingersCrossedHandler');
420+
$this->assertDICConstructorArguments($handler, array(new Reference('monolog.handler.nested'), new Reference('monolog.handler.main.http_code_strategy'), 0, true, true, null));
421+
}
422+
386423
protected function getContainer(array $config = array(), array $thirdPartyDefinitions = array())
387424
{
388425
$container = new ContainerBuilder();

0 commit comments

Comments
 (0)