Added placeholders to all log messages instead of hardcoded values

This commit is contained in:
Martin Hasoň 2015-10-16 16:24:48 +02:00
parent 586c58a354
commit c92fcdb273
6 changed files with 20 additions and 21 deletions

View File

@ -259,7 +259,7 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
$info = $this->getListenerInfo($listener->getWrappedListener(), $eventName);
if ($listener->wasCalled()) {
if (null !== $this->logger) {
$this->logger->debug(sprintf('Notified event "%s" to listener "%s".', $eventName, $info['pretty']));
$this->logger->debug('Notified event "{event}" to listener "{listener}".', array('event' => $eventName, 'listener' => $info['pretty']));
}
if (!isset($this->called[$eventName])) {
@ -270,12 +270,12 @@ class TraceableEventDispatcher implements TraceableEventDispatcherInterface
}
if (null !== $this->logger && $skipped) {
$this->logger->debug(sprintf('Listener "%s" was not called for event "%s".', $info['pretty'], $eventName));
$this->logger->debug('Listener "{listener}" was not called for event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName));
}
if ($listener->stoppedPropagation()) {
if (null !== $this->logger) {
$this->logger->debug(sprintf('Listener "%s" stopped propagation of the event "%s".', $info['pretty'], $eventName));
$this->logger->debug('Listener "{listener}" stopped propagation of the event "{event}".', array('listener' => $info['pretty'], 'event' => $eventName));
}
$skipped = true;

View File

@ -110,8 +110,8 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
$tdispatcher->addListener('foo', $listener1 = function () {});
$tdispatcher->addListener('foo', $listener2 = function () {});
$logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".');
$logger->expects($this->at(1))->method('debug')->with('Notified event "foo" to listener "closure".');
$logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
$logger->expects($this->at(1))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
$tdispatcher->dispatch('foo');
}
@ -125,9 +125,9 @@ class TraceableEventDispatcherTest extends \PHPUnit_Framework_TestCase
$tdispatcher->addListener('foo', $listener1 = function (Event $event) { $event->stopPropagation(); });
$tdispatcher->addListener('foo', $listener2 = function () {});
$logger->expects($this->at(0))->method('debug')->with('Notified event "foo" to listener "closure".');
$logger->expects($this->at(1))->method('debug')->with('Listener "closure" stopped propagation of the event "foo".');
$logger->expects($this->at(2))->method('debug')->with('Listener "closure" was not called for event "foo".');
$logger->expects($this->at(0))->method('debug')->with('Notified event "{event}" to listener "{listener}".', array('event' => 'foo', 'listener' => 'closure'));
$logger->expects($this->at(1))->method('debug')->with('Listener "{listener}" stopped propagation of the event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
$logger->expects($this->at(2))->method('debug')->with('Listener "{listener}" was not called for event "{event}".', array('event' => 'foo', 'listener' => 'closure'));
$tdispatcher->dispatch('foo');
}

View File

@ -104,7 +104,8 @@ class RouterListener implements EventSubscriberInterface
}
if (null !== $this->logger) {
$this->logger->info(sprintf('Matched route "%s".', isset($parameters['_route']) ? $parameters['_route'] : 'n/a'), array(
$this->logger->info('Matched route "{route}".', array(
'route' => isset($parameters['_route']) ? $parameters['_route'] : 'n/a',
'route_parameters' => $parameters,
'request_uri' => $request->getUri(),
));

View File

@ -132,7 +132,7 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider getLoggingParameterData
*/
public function testLoggingParameter($parameter, $log)
public function testLoggingParameter($parameter, $log, $parameters)
{
$requestMatcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
$requestMatcher->expects($this->once())
@ -142,7 +142,7 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('info')
->with($this->equalTo($log));
->with($this->equalTo($log), $this->equalTo($parameters));
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$request = Request::create('http://localhost/');
@ -154,8 +154,8 @@ class RouterListenerTest extends \PHPUnit_Framework_TestCase
public function getLoggingParameterData()
{
return array(
array(array('_route' => 'foo'), 'Matched route "foo".'),
array(array(), 'Matched route "n/a".'),
array(array('_route' => 'foo'), 'Matched route "{route}".', array('route' => 'foo', 'route_parameters' => array('_route' => 'foo'), 'request_uri' => 'http://localhost/')),
array(array(), 'Matched route "{route}".', array('route' => 'n/a', 'route_parameters' => array(), 'request_uri' => 'http://localhost/')),
);
}
}

View File

@ -153,18 +153,18 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
$url = '';
$optional = true;
$message = 'Parameter "{parameter}" for route "{route}" must match "{expected}" ("{given}" given) to generate a corresponding URL.';
foreach ($tokens as $token) {
if ('variable' === $token[0]) {
if (!$optional || !array_key_exists($token[3], $defaults) || null !== $mergedParams[$token[3]] && (string) $mergedParams[$token[3]] !== (string) $defaults[$token[3]]) {
// check requirement
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#', $mergedParams[$token[3]])) {
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
if ($this->strictRequirements) {
throw new InvalidParameterException($message);
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]])));
}
if ($this->logger) {
$this->logger->error($message);
$this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]]));
}
return;
@ -222,14 +222,12 @@ class UrlGenerator implements UrlGeneratorInterface, ConfigurableRequirementsInt
foreach ($hostTokens as $token) {
if ('variable' === $token[0]) {
if (null !== $this->strictRequirements && !preg_match('#^'.$token[2].'$#i', $mergedParams[$token[3]])) {
$message = sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given) to generate a corresponding URL.', $token[3], $name, $token[2], $mergedParams[$token[3]]);
if ($this->strictRequirements) {
throw new InvalidParameterException($message);
throw new InvalidParameterException(strtr($message, array('{parameter}' => $token[3], '{route}' => $name, '{expected}' => $token[2], '{given}' => $mergedParams[$token[3]])));
}
if ($this->logger) {
$this->logger->error($message);
$this->logger->error($message, array('parameter' => $token[3], 'route' => $name, 'expected' => $token[2], 'given' => $mergedParams[$token[3]]));
}
return;

View File

@ -78,7 +78,7 @@ class GuardAuthenticationListener implements ListenerInterface
if ($event->hasResponse()) {
if (null !== $this->logger) {
$this->logger->debug(sprintf('The "%s" authenticator set the response. Any later authenticator will not be called', get_class($guardAuthenticator)));
$this->logger->debug('The "{authenticator}" authenticator set the response. Any later authenticator will not be called', array('authenticator' => get_class($guardAuthenticator)));
}
break;