14
14
use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
15
15
use Symfony \Component \DependencyInjection \ContainerBuilder ;
16
16
use Symfony \Component \DependencyInjection \Reference ;
17
+ use Symfony \Component \HttpKernel \Log \Logger ;
17
18
18
19
class AddDebugLogProcessorPass implements CompilerPassInterface
19
20
{
@@ -22,22 +23,38 @@ public function process(ContainerBuilder $container)
22
23
if (!$ container ->hasDefinition ('profiler ' )) {
23
24
return ;
24
25
}
25
- if (!$ container ->hasDefinition ('monolog.logger_prototype ' )) {
26
+
27
+ if ($ container ->hasDefinition ('monolog.logger_prototype ' ) && $ container ->hasDefinition ('debug.log_processor ' )) {
28
+ $ container ->getDefinition ('monolog.logger_prototype ' )
29
+ ->setConfigurator ([__CLASS__ , 'configureMonologLogger ' ])
30
+ ->addMethodCall ('pushProcessor ' , [new Reference ('debug.log_processor ' )])
31
+ ;
32
+
26
33
return ;
27
34
}
28
- if (!$ container ->hasDefinition ('debug.log_processor ' )) {
35
+
36
+ if (!$ container ->hasDefinition ('logger ' )) {
29
37
return ;
30
38
}
31
39
32
- $ definition = $ container ->getDefinition ('monolog.logger_prototype ' );
33
- $ definition ->setConfigurator ([__CLASS__ , 'configureLogger ' ]);
34
- $ definition ->addMethodCall ('pushProcessor ' , [new Reference ('debug.log_processor ' )]);
40
+ $ loggerDefinition = $ container ->getDefinition ('logger ' );
41
+
42
+ if (Logger::class === $ loggerDefinition ->getClass ()) {
43
+ $ loggerDefinition ->setConfigurator ([__CLASS__ , 'configureHttpKernelLogger ' ]);
44
+ }
35
45
}
36
46
37
- public static function configureLogger (mixed $ logger )
47
+ public static function configureMonologLogger (mixed $ logger )
38
48
{
39
- if (\is_object ( $ logger ) && method_exists ( $ logger , ' removeDebugLogger ' ) && \ in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true )) {
49
+ if (\in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true ) && \is_object ( $ logger ) && method_exists ( $ logger , ' removeDebugLogger ' )) {
40
50
$ logger ->removeDebugLogger ();
41
51
}
42
52
}
53
+
54
+ public static function configureHttpKernelLogger (Logger $ logger )
55
+ {
56
+ if (!\in_array (\PHP_SAPI , ['cli ' , 'phpdbg ' ], true ) && method_exists ($ logger , 'enableDebug ' )) {
57
+ $ logger ->enableDebug ();
58
+ }
59
+ }
43
60
}
0 commit comments