Skip to content

Commit 8f00123

Browse files
gedimin45fabpot
authored andcommitted
ability to configure whether messages should be formatted as PSR-3
1 parent 6728c60 commit 8f00123

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ public function getConfigTreeBuilder()
311311
->booleanNode('bubble')->defaultTrue()->end()
312312
->scalarNode('app_name')->defaultNull()->end()
313313
->booleanNode('include_stacktraces')->defaultFalse()->end()
314+
->booleanNode('process_psr_3_messages')->defaultTrue()->end()
314315
->scalarNode('path')->defaultValue('%kernel.logs_dir%/%kernel.environment%.log')->end() // stream and rotating
315316
->scalarNode('file_permission') // stream and rotating
316317
->defaultNull()

DependencyInjection/MonologExtension.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
143143
$definition->setConfigurator(array('Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces'));
144144
}
145145

146+
if ($handler['process_psr_3_messages']) {
147+
$processorId = 'monolog.processor.psr_log_message';
148+
if (!$container->hasDefinition($processorId)) {
149+
$processor = new Definition('Monolog\\Processor\\PsrLogMessageProcessor');
150+
$processor->setPublic(false);
151+
$container->setDefinition($processorId, $processor);
152+
}
153+
154+
$definition->addMethodCall('pushProcessor', array(new Reference($processorId)));
155+
}
156+
146157
switch ($handler['type']) {
147158
case 'stream':
148159
$definition->setArguments(array(

Resources/config/schema/monolog-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<xsd:attribute name="priority" type="xsd:integer" />
3333
<xsd:attribute name="level" type="level" />
3434
<xsd:attribute name="bubble" type="xsd:boolean" />
35+
<xsd:attribute name="process-psr-3-messages" type="xsd:boolean" />
3536
<xsd:attribute name="app-name" type="xsd:string" />
3637
<xsd:attribute name="path" type="xsd:string" />
3738
<xsd:attribute name="id" type="xsd:string" />

Tests/DependencyInjection/FixtureMonologExtensionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bundle\MonologBundle\DependencyInjection\MonologExtension;
1515
use Symfony\Bundle\MonologBundle\DependencyInjection\Compiler\LoggerChannelPass;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
1718
use Symfony\Component\DependencyInjection\Reference;
1819

1920
abstract class FixtureMonologExtensionTest extends DependencyInjectionTest
@@ -178,6 +179,22 @@ public function testChannelParametersResolved()
178179
);
179180
}
180181

182+
public function testPsr3MessageProcessingDisabled()
183+
{
184+
$container = $this->getContainer('process_psr_3_messages_disabled');
185+
186+
$logger = $container->getDefinition('monolog.handler.custom');
187+
188+
$methodCalls = $logger->getMethodCalls();
189+
190+
foreach ($methodCalls as $methodCall) {
191+
list($methodName, $params) = $methodCall;
192+
if ($methodName === 'pushProcessor') {
193+
$this->assertNotEquals(array(new Definition('monolog.processor.psr_log_message')), $params);
194+
}
195+
}
196+
}
197+
181198
protected function getContainer($fixture)
182199
{
183200
$container = new ContainerBuilder();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
<monolog:config>
9+
<monolog:handler name="custom" type="stream" path="/tmp/symfony.log" process-psr-3-messages="false"/>
10+
</monolog:config>
11+
</container>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
monolog:
2+
handlers:
3+
custom:
4+
type: stream
5+
path: /tmp/symfony.log
6+
process_psr_3_messages: false

Tests/DependencyInjection/MonologExtensionTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function testLoadWithDefault()
3434
$handler = $container->getDefinition('monolog.handler.main');
3535
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\StreamHandler');
3636
$this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null));
37+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
3738
}
3839

3940
public function testLoadWithCustomValues()
@@ -248,10 +249,10 @@ public function testSocketHandler()
248249
$handler = $container->getDefinition('monolog.handler.socket');
249250
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\SocketHandler');
250251
$this->assertDICConstructorArguments($handler, array('localhost:50505', \Monolog\Logger::DEBUG, true));
251-
$this->assertDICDefinitionMethodCallAt(0, $handler, 'setTimeout', array('1'));
252-
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setConnectionTimeout', array('0.6'));
253-
$this->assertDICDefinitionMethodCallAt(2, $handler, 'setPersistent', array(true));
254-
252+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
253+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setTimeout', array('1'));
254+
$this->assertDICDefinitionMethodCallAt(2, $handler, 'setConnectionTimeout', array('0.6'));
255+
$this->assertDICDefinitionMethodCallAt(3, $handler, 'setPersistent', array(true));
255256
}
256257

257258
public function testRavenHandlerWhenConfigurationIsWrong()
@@ -347,13 +348,14 @@ public function testLogglyHandler()
347348
$handler = $container->getDefinition('monolog.handler.loggly');
348349
$this->assertDICDefinitionClass($handler, 'Monolog\Handler\LogglyHandler');
349350
$this->assertDICConstructorArguments($handler, array($token, \Monolog\Logger::DEBUG, true));
350-
$this->assertEmpty($handler->getMethodCalls());
351+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
351352

352353
$container = $this->getContainer(array(array('handlers' => array('loggly' => array(
353354
'type' => 'loggly', 'token' => $token, 'tags' => array(' ', 'foo', '', 'bar'))
354355
))));
355356
$handler = $container->getDefinition('monolog.handler.loggly');
356-
$this->assertDICDefinitionMethodCallAt(0, $handler, 'setTag', array('foo,bar'));
357+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
358+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setTag', array('foo,bar'));
357359
}
358360

359361
public function testFingersCrossedHandlerWhenExcluded404sAreSpecified()

0 commit comments

Comments
 (0)