TestHandler.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\Logger;
  12. /**
  13. * Used for testing purposes.
  14. *
  15. * It records all records and gives you access to them for verification.
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. */
  19. class TestHandler extends AbstractHandler
  20. {
  21. protected $records;
  22. protected $recordsByLevel;
  23. public function getRecords()
  24. {
  25. return $this->records;
  26. }
  27. public function hasError($record)
  28. {
  29. return $this->hasRecord($record, Logger::ERROR);
  30. }
  31. public function hasWarning($record)
  32. {
  33. return $this->hasRecord($record, Logger::WARNING);
  34. }
  35. public function hasInfo($record)
  36. {
  37. return $this->hasRecord($record, Logger::INFO);
  38. }
  39. public function hasDebug($record)
  40. {
  41. return $this->hasRecord($record, Logger::DEBUG);
  42. }
  43. public function hasErrorrecords()
  44. {
  45. return isset($this->recordsByLevel[Logger::ERROR]);
  46. }
  47. public function hasWarningrecords()
  48. {
  49. return isset($this->recordsByLevel[Logger::WARNING]);
  50. }
  51. public function hasInforecords()
  52. {
  53. return isset($this->recordsByLevel[Logger::INFO]);
  54. }
  55. public function hasDebugrecords()
  56. {
  57. return isset($this->recordsByLevel[Logger::DEBUG]);
  58. }
  59. protected function hasRecord($record, $level = null)
  60. {
  61. if (null === $level) {
  62. $records = $this->records;
  63. } else {
  64. $records = $this->recordsByLevel[$level];
  65. }
  66. foreach ($records as $msg) {
  67. if ($msg['message'] === $record) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. public function write($record)
  74. {
  75. $this->recordsByLevel[$record['level']][] = $record;
  76. $this->records[] = $record;
  77. }
  78. }