Skip to content

Commit 23b672f

Browse files
committed
feature #21396 [DI] Enhance logging in compiler passes (nicolas-grekas)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DI] Enhance logging in compiler passes | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - We should log more in compiler passes - and this should be better integrated in usual log reports. For logging more, let's drop LoggingFormatter and add a simple "log" method on ContainerBuilder. For better integration, let's throw silenced notices - they can be caught by our Debug handler. Commits ------- fb200a0d2f [DI] Enhance logging in compiler passes
2 parents 0750749 + 0743971 commit 23b672f

10 files changed

+41
-17
lines changed

Compiler/AutowirePass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,7 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass, array $
150150
}
151151

152152
if ($notFound = array_diff($autowiredMethods, $found)) {
153-
$compiler = $this->container->getCompiler();
154-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUnusedAutowiringPatterns($this, $this->currentId, $notFound));
153+
$this->container->log($this, sprintf('Autowiring\'s patterns "%s" for service "%s" don\'t match any method.', implode('", "', $notFound), $this->currentId));
155154
}
156155

157156
return $methodsToAutowire;

Compiler/Compiler.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function __construct()
3030
{
3131
$this->passConfig = new PassConfig();
3232
$this->serviceReferenceGraph = new ServiceReferenceGraph();
33-
$this->loggingFormatter = new LoggingFormatter();
3433
}
3534

3635
/**
@@ -57,9 +56,17 @@ public function getServiceReferenceGraph()
5756
* Returns the logging formatter which can be used by compilation passes.
5857
*
5958
* @return LoggingFormatter
59+
*
60+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
6061
*/
6162
public function getLoggingFormatter()
6263
{
64+
if (null === $this->loggingFormatter) {
65+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
66+
67+
$this->loggingFormatter = new LoggingFormatter();
68+
}
69+
6370
return $this->loggingFormatter;
6471
}
6572

@@ -92,12 +99,24 @@ public function addPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BE
9299
* Adds a log message.
93100
*
94101
* @param string $string The log message
102+
*
103+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
95104
*/
96105
public function addLogMessage($string)
97106
{
107+
@trigger_error(sprintf('The %s() method is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', __METHOD__), E_USER_DEPRECATED);
108+
98109
$this->log[] = $string;
99110
}
100111

112+
/**
113+
* @final
114+
*/
115+
public function log(CompilerPassInterface $pass, $message)
116+
{
117+
$this->log[] = get_class($pass).': '.$message;
118+
}
119+
101120
/**
102121
* Returns the log.
103122
*

Compiler/InlineServiceDefinitionsPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,10 @@ protected function processValue($value, $isRoot = false)
4343
return $value;
4444
}
4545
if ($value instanceof Reference && $this->container->hasDefinition($id = (string) $value)) {
46-
$compiler = $this->container->getCompiler();
4746
$definition = $this->container->getDefinition($id);
4847

49-
if ($this->isInlineableDefinition($id, $definition, $compiler->getServiceReferenceGraph())) {
50-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatInlineService($this, $id, $this->currentId));
48+
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
49+
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
5150

5251
if ($definition->isShared()) {
5352
return $definition;

Compiler/LoggingFormatter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Compiler;
1313

14+
@trigger_error('The '.__NAMESPACE__.'\LoggingFormatter class is deprecated since version 3.3 and will be removed in 4.0. Use the ContainerBuilder::log() method instead.', E_USER_DEPRECATED);
15+
1416
/**
1517
* Used to format logging messages during the compilation.
1618
*
1719
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
20+
*
21+
* @deprecated since version 3.3, to be removed in 4.0. Use the ContainerBuilder::log() method instead.
1822
*/
1923
class LoggingFormatter
2024
{

Compiler/RemoveAbstractDefinitionsPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ class RemoveAbstractDefinitionsPass implements CompilerPassInterface
2626
public function process(ContainerBuilder $container)
2727
{
2828
$compiler = $container->getCompiler();
29-
$formatter = $compiler->getLoggingFormatter();
3029

3130
foreach ($container->getDefinitions() as $id => $definition) {
3231
if ($definition->isAbstract()) {
3332
$container->removeDefinition($id);
34-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'abstract'));
33+
$container->log($this, sprintf('Removed service "%s"; reason: abstract.', $id));
3534
}
3635
}
3736
}

Compiler/RemovePrivateAliasesPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,14 @@ class RemovePrivateAliasesPass implements CompilerPassInterface
3030
public function process(ContainerBuilder $container)
3131
{
3232
$compiler = $container->getCompiler();
33-
$formatter = $compiler->getLoggingFormatter();
3433

3534
foreach ($container->getAliases() as $id => $alias) {
3635
if ($alias->isPublic()) {
3736
continue;
3837
}
3938

4039
$container->removeAlias($id);
41-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'private alias'));
40+
$container->log($this, sprintf('Removed service "%s"; reason: private alias.', $id));
4241
}
4342
}
4443
}

Compiler/RemoveUnusedDefinitionsPass.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public function setRepeatedPass(RepeatedPass $repeatedPass)
3838
public function process(ContainerBuilder $container)
3939
{
4040
$compiler = $container->getCompiler();
41-
$formatter = $compiler->getLoggingFormatter();
4241
$graph = $compiler->getServiceReferenceGraph();
4342

4443
$hasChanged = false;
@@ -69,10 +68,10 @@ public function process(ContainerBuilder $container)
6968
$container->setDefinition((string) reset($referencingAliases), $definition);
7069
$definition->setPublic(true);
7170
$container->removeDefinition($id);
72-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'replaces alias '.reset($referencingAliases)));
71+
$container->log($this, sprintf('Removed service "%s"; reason: replaces alias %s.', $id, reset($referencingAliases)));
7372
} elseif (0 === count($referencingAliases) && false === $isReferenced) {
7473
$container->removeDefinition($id);
75-
$compiler->addLogMessage($formatter->formatRemoveService($this, $id, 'unused'));
74+
$container->log($this, sprintf('Removed service "%s"; reason: unused.', $id));
7675
$hasChanged = true;
7776
}
7877
}

Compiler/ReplaceAliasByActualDefinitionPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ protected function processValue($value, $isRoot = false)
8282
// Perform the replacement
8383
$newId = $this->replacements[$referenceId];
8484
$value = new Reference($newId, $value->getInvalidBehavior());
85-
$compiler = $this->container->getCompiler();
86-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatUpdateReference($this, $this->currentId, $referenceId, $newId));
85+
$this->container->log($this, sprintf('Changed reference of service "%s" previously pointing to "%s" to "%s".', $this->currentId, $referenceId, $newId));
8786
}
8887

8988
return parent::processValue($value, $isRoot);

Compiler/ResolveDefinitionTemplatesPass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ private function doResolveDefinition(ChildDefinition $definition)
8080
$this->currentId = $id;
8181
}
8282

83-
$compiler = $this->container->getCompiler();
84-
$compiler->addLogMessage($compiler->getLoggingFormatter()->formatResolveInheritance($this, $this->currentId, $parent));
83+
$this->container->log($this, sprintf('Resolving inheritance for "%s" (parent: %s).', $this->currentId, $parent));
8584
$def = new Definition();
8685

8786
// merge in parent definition

ContainerBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,6 +1171,14 @@ public function getNormalizedIds()
11711171
return $normalizedIds;
11721172
}
11731173

1174+
/**
1175+
* @final
1176+
*/
1177+
public function log(CompilerPassInterface $pass, $message)
1178+
{
1179+
$this->getCompiler()->log($pass, $message);
1180+
}
1181+
11741182
/**
11751183
* Returns the Service Conditionals.
11761184
*

0 commit comments

Comments
 (0)