vendor/symfony/monolog-bridge/Processor/DebugProcessor.php line 21

Open in your IDE?
  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\Bridge\Monolog\Processor;
  11. use Monolog\Logger;
  12. use Monolog\LogRecord;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  16. use Symfony\Contracts\Service\ResetInterface;
  17. class DebugProcessor implements DebugLoggerInterface, ResetInterface
  18. {
  19. use CompatibilityProcessor;
  20. private array $records = [];
  21. private array $errorCount = [];
  22. private ?RequestStack $requestStack;
  23. public function __construct(?RequestStack $requestStack = null)
  24. {
  25. $this->requestStack = $requestStack;
  26. }
  27. private function doInvoke(array|LogRecord $record): array|LogRecord
  28. {
  29. $key = $this->requestStack && ($request = $this->requestStack->getCurrentRequest()) ? spl_object_id($request) : '';
  30. $timestampRfc3339 = false;
  31. if ($record['datetime'] instanceof \DateTimeInterface) {
  32. $timestamp = $record['datetime']->getTimestamp();
  33. $timestampRfc3339 = $record['datetime']->format(\DateTimeInterface::RFC3339_EXTENDED);
  34. } elseif (false !== $timestamp = strtotime($record['datetime'])) {
  35. $timestampRfc3339 = (new \DateTimeImmutable($record['datetime']))->format(\DateTimeInterface::RFC3339_EXTENDED);
  36. }
  37. $this->records[$key][] = [
  38. 'timestamp' => $timestamp,
  39. 'timestamp_rfc3339' => $timestampRfc3339,
  40. 'message' => $record['message'],
  41. 'priority' => $record['level'],
  42. 'priorityName' => $record['level_name'],
  43. 'context' => $record['context'],
  44. 'channel' => $record['channel'] ?? '',
  45. ];
  46. if (!isset($this->errorCount[$key])) {
  47. $this->errorCount[$key] = 0;
  48. }
  49. switch ($record['level']) {
  50. case Logger::ERROR:
  51. case Logger::CRITICAL:
  52. case Logger::ALERT:
  53. case Logger::EMERGENCY:
  54. ++$this->errorCount[$key];
  55. }
  56. return $record;
  57. }
  58. public function getLogs(?Request $request = null): array
  59. {
  60. if (null !== $request) {
  61. return $this->records[spl_object_id($request)] ?? [];
  62. }
  63. if (!$this->records) {
  64. return [];
  65. }
  66. return array_merge(...array_values($this->records));
  67. }
  68. public function countErrors(?Request $request = null): int
  69. {
  70. if (null !== $request) {
  71. return $this->errorCount[spl_object_id($request)] ?? 0;
  72. }
  73. return array_sum($this->errorCount);
  74. }
  75. public function clear(): void
  76. {
  77. $this->records = [];
  78. $this->errorCount = [];
  79. }
  80. /**
  81. * @return void
  82. */
  83. public function reset()
  84. {
  85. $this->clear();
  86. }
  87. }