TestHandler.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 AbstractProcessingHandler
  20. {
  21. protected $records = array();
  22. protected $recordsByLevel = array();
  23. public function getRecords()
  24. {
  25. return $this->records;
  26. }
  27. public function hasAlert($record)
  28. {
  29. return $this->hasRecord($record, Logger::ALERT);
  30. }
  31. public function hasCritical($record)
  32. {
  33. return $this->hasRecord($record, Logger::CRITICAL);
  34. }
  35. public function hasError($record)
  36. {
  37. return $this->hasRecord($record, Logger::ERROR);
  38. }
  39. public function hasWarning($record)
  40. {
  41. return $this->hasRecord($record, Logger::WARNING);
  42. }
  43. public function hasInfo($record)
  44. {
  45. return $this->hasRecord($record, Logger::INFO);
  46. }
  47. public function hasDebug($record)
  48. {
  49. return $this->hasRecord($record, Logger::DEBUG);
  50. }
  51. public function hasAlertRecords()
  52. {
  53. return isset($this->recordsByLevel[Logger::ALERT]);
  54. }
  55. public function hasCriticalRecords()
  56. {
  57. return isset($this->recordsByLevel[Logger::CRITICAL]);
  58. }
  59. public function hasErrorRecords()
  60. {
  61. return isset($this->recordsByLevel[Logger::ERROR]);
  62. }
  63. public function hasWarningRecords()
  64. {
  65. return isset($this->recordsByLevel[Logger::WARNING]);
  66. }
  67. public function hasInfoRecords()
  68. {
  69. return isset($this->recordsByLevel[Logger::INFO]);
  70. }
  71. public function hasDebugRecords()
  72. {
  73. return isset($this->recordsByLevel[Logger::DEBUG]);
  74. }
  75. protected function hasRecord($record, $level)
  76. {
  77. if (!isset($this->recordsByLevel[$level])) {
  78. return false;
  79. }
  80. if (is_array($record)) {
  81. $record = $record['message'];
  82. }
  83. foreach ($this->recordsByLevel[$level] as $rec) {
  84. if ($rec['message'] === $record) {
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. protected function write(array $record)
  94. {
  95. $this->recordsByLevel[$record['level']][] = $record;
  96. $this->records[] = $record;
  97. }
  98. }