Skip to content

Commit 6acef3b

Browse files
committed
bug #173 ability to configure whether messages should be formatted as PSR-3 (ged15)
This PR was submitted for the master branch but it was merged into the 2.x branch instead (closes #173). Discussion ---------- ability to configure whether messages should be formatted as PSR-3 This PR symfony/symfony#17166 (reasons described in symfony/symfony#15753) has changed the way messages are logged in Symfony. Now log messages contain placeholders (see https://github.com/symfony/symfony-standard/issues/981). While this could be a useful feature, IMO by default in SE messages should be preformatted. This PR introduces a new optional config option that makes the messages be preformatted by default but this behaviour can also be disabled. Commits ------- 6c8a44b ability to configure whether messages should be formatted as PSR-3 44f3daf minor #179 removed obsolete code (fabpot) 7d28c5a Merge branch '2.x' b6baaaf Merge branch '2.x' 57b33d6 removed obsolete code a45682c updated CHANGELOG bcbf4c5 fixed tests 079c3d1 feature #170 remove class parameters (avant1) 6398efc remove class parameters 885da32 added a CHANGELOG d8b3f8a feature #169 Deprecate the NotFoundActivationStrategy class (BPScott) b77bdf0 Deprecate the NotFoundActivationStrategy class ce169ee bumped version to 3.x
1 parent 6d5f900 commit 6acef3b

File tree

7 files changed

+55
-7
lines changed

7 files changed

+55
-7
lines changed

DependencyInjection/Configuration.php

+1
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

+11
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,17 @@ private function buildHandler(ContainerBuilder $container, $name, array $handler
136136
$definition->setConfigurator(array('Symfony\\Bundle\\MonologBundle\\MonologBundle', 'includeStacktraces'));
137137
}
138138

139+
if ($handler['process_psr_3_messages']) {
140+
$processorId = 'monolog.processor.psr_log_message';
141+
if (!$container->hasDefinition($processorId)) {
142+
$processor = new Definition('Monolog\\Processor\\PsrLogMessageProcessor');
143+
$processor->setPublic(false);
144+
$container->setDefinition($processorId, $processor);
145+
}
146+
147+
$definition->addMethodCall('pushProcessor', array(new Reference($processorId)));
148+
}
149+
139150
switch ($handler['type']) {
140151
case 'service':
141152
$container->setAlias($handlerId, $handler['id']);

Resources/config/schema/monolog-1.0.xsd

+1
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

+17
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();
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>
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

+8-7
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function testLoadWithDefault()
3333
$handler = $container->getDefinition('monolog.handler.main');
3434
$this->assertDICDefinitionClass($handler, '%monolog.handler.stream.class%');
3535
$this->assertDICConstructorArguments($handler, array('%kernel.logs_dir%/%kernel.environment%.log', \Monolog\Logger::DEBUG, true, null));
36+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
3637
}
3738

3839
public function testLoadWithCustomValues()
@@ -232,10 +233,10 @@ public function testSocketHandler()
232233
$handler = $container->getDefinition('monolog.handler.socket');
233234
$this->assertDICDefinitionClass($handler, '%monolog.handler.socket.class%');
234235
$this->assertDICConstructorArguments($handler, array('localhost:50505', \Monolog\Logger::DEBUG, true));
235-
$this->assertDICDefinitionMethodCallAt(0, $handler, 'setTimeout', array('1'));
236-
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setConnectionTimeout', array('0.6'));
237-
$this->assertDICDefinitionMethodCallAt(2, $handler, 'setPersistent', array(true));
238-
236+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
237+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setTimeout', array('1'));
238+
$this->assertDICDefinitionMethodCallAt(2, $handler, 'setConnectionTimeout', array('0.6'));
239+
$this->assertDICDefinitionMethodCallAt(3, $handler, 'setPersistent', array(true));
239240
}
240241

241242
public function testRavenHandlerWhenConfigurationIsWrong()
@@ -331,14 +332,14 @@ public function testLogglyHandler()
331332
$handler = $container->getDefinition('monolog.handler.loggly');
332333
$this->assertDICDefinitionClass($handler, '%monolog.handler.loggly.class%');
333334
$this->assertDICConstructorArguments($handler, array($token, \Monolog\Logger::DEBUG, true));
334-
$this->assertEmpty($handler->getMethodCalls());
335+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
335336

336337
$container = $this->getContainer(array(array('handlers' => array('loggly' => array(
337338
'type' => 'loggly', 'token' => $token, 'tags' => array(' ', 'foo', '', 'bar'))
338339
))));
339340
$handler = $container->getDefinition('monolog.handler.loggly');
340-
$this->assertDICDefinitionMethodCallAt(0, $handler, 'setTag', array('foo,bar'));
341-
341+
$this->assertDICDefinitionMethodCallAt(0, $handler, 'pushProcessor', array(new Reference('monolog.processor.psr_log_message')));
342+
$this->assertDICDefinitionMethodCallAt(1, $handler, 'setTag', array('foo,bar'));
342343
}
343344

344345
protected function getContainer(array $config = array())

0 commit comments

Comments
 (0)