DebugLoggerConfigurator.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Log;
  11. use Monolog\Logger;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. */
  15. class DebugLoggerConfigurator
  16. {
  17. private ?object $processor = null;
  18. public function __construct(callable $processor, ?bool $enable = null)
  19. {
  20. if ($enable ?? !\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
  21. $this->processor = \is_object($processor) ? $processor : $processor(...);
  22. }
  23. }
  24. public function pushDebugLogger(Logger $logger): void
  25. {
  26. if ($this->processor) {
  27. $logger->pushProcessor($this->processor);
  28. }
  29. }
  30. public static function getDebugLogger(mixed $logger): ?DebugLoggerInterface
  31. {
  32. if ($logger instanceof DebugLoggerInterface) {
  33. return $logger;
  34. }
  35. if (!$logger instanceof Logger) {
  36. return null;
  37. }
  38. foreach ($logger->getProcessors() as $processor) {
  39. if ($processor instanceof DebugLoggerInterface) {
  40. return $processor;
  41. }
  42. }
  43. return null;
  44. }
  45. }