LoggerDataCollector.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. /**
  14. * LogDataCollector.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class LoggerDataCollector extends DataCollector
  19. {
  20. protected $logger;
  21. public function __construct($logger = null)
  22. {
  23. if (null !== $logger) {
  24. $this->logger = $logger->getDebugLogger();
  25. }
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function collect(Request $request, Response $response, \Exception $exception = null)
  31. {
  32. if (null !== $this->logger) {
  33. $this->data = array(
  34. 'error_count' => $this->logger->countErrors(),
  35. 'logs' => $this->logger->getLogs(),
  36. );
  37. }
  38. }
  39. /**
  40. * Gets the called events.
  41. *
  42. * @return array An array of called events
  43. *
  44. * @see EventDispatcherTraceableInterface
  45. */
  46. public function countErrors()
  47. {
  48. return isset($this->data['error_count']) ? $this->data['error_count'] : 0;
  49. }
  50. /**
  51. * Gets the logs.
  52. *
  53. * @return array An array of logs
  54. */
  55. public function getLogs()
  56. {
  57. return isset($this->data['logs']) ? $this->data['logs'] : array();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function getName()
  63. {
  64. return 'logger';
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getStatus()
  70. {
  71. return 0 === $this->countErrors() ? self::INFO : self::ERROR;
  72. }
  73. }