Logger.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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;
  11. use Monolog\Logger as BaseLogger;
  12. use Monolog\ResettableInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Logger extends BaseLogger implements DebugLoggerInterface, ResetInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function getLogs(Request $request = null)
  25. {
  26. if ($logger = $this->getDebugLogger()) {
  27. return $logger->getLogs($request);
  28. }
  29. return [];
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function countErrors(Request $request = null)
  35. {
  36. if ($logger = $this->getDebugLogger()) {
  37. return $logger->countErrors($request);
  38. }
  39. return 0;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function clear()
  45. {
  46. if ($logger = $this->getDebugLogger()) {
  47. $logger->clear();
  48. }
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function reset(): void
  54. {
  55. $this->clear();
  56. if ($this instanceof ResettableInterface) {
  57. parent::reset();
  58. }
  59. }
  60. public function removeDebugLogger()
  61. {
  62. foreach ($this->processors as $k => $processor) {
  63. if ($processor instanceof DebugLoggerInterface) {
  64. unset($this->processors[$k]);
  65. }
  66. }
  67. foreach ($this->handlers as $k => $handler) {
  68. if ($handler instanceof DebugLoggerInterface) {
  69. unset($this->handlers[$k]);
  70. }
  71. }
  72. }
  73. /**
  74. * Returns a DebugLoggerInterface instance if one is registered with this logger.
  75. */
  76. private function getDebugLogger(): ?DebugLoggerInterface
  77. {
  78. foreach ($this->processors as $processor) {
  79. if ($processor instanceof DebugLoggerInterface) {
  80. return $processor;
  81. }
  82. }
  83. foreach ($this->handlers as $handler) {
  84. if ($handler instanceof DebugLoggerInterface) {
  85. return $handler;
  86. }
  87. }
  88. return null;
  89. }
  90. }