DeduplicationHandlerTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php declare(strict_types=1);
  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\Test\TestCase;
  12. use Monolog\Logger;
  13. class DeduplicationHandlerTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Handler\DeduplicationHandler::flush
  17. */
  18. public function testFlushPassthruIfAllRecordsUnderTrigger()
  19. {
  20. $test = new TestHandler();
  21. @unlink(sys_get_temp_dir().'/monolog_dedup.log');
  22. $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
  23. $handler->handle($this->getRecord(Logger::DEBUG));
  24. $handler->handle($this->getRecord(Logger::INFO));
  25. $handler->flush();
  26. $this->assertTrue($test->hasInfoRecords());
  27. $this->assertTrue($test->hasDebugRecords());
  28. $this->assertFalse($test->hasWarningRecords());
  29. }
  30. /**
  31. * @covers Monolog\Handler\DeduplicationHandler::flush
  32. * @covers Monolog\Handler\DeduplicationHandler::appendRecord
  33. */
  34. public function testFlushPassthruIfEmptyLog()
  35. {
  36. $test = new TestHandler();
  37. @unlink(sys_get_temp_dir().'/monolog_dedup.log');
  38. $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
  39. $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
  40. $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
  41. $handler->flush();
  42. $this->assertTrue($test->hasErrorRecords());
  43. $this->assertTrue($test->hasCriticalRecords());
  44. $this->assertFalse($test->hasWarningRecords());
  45. }
  46. /**
  47. * @covers Monolog\Handler\DeduplicationHandler::flush
  48. * @covers Monolog\Handler\DeduplicationHandler::appendRecord
  49. * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
  50. * @depends testFlushPassthruIfEmptyLog
  51. */
  52. public function testFlushSkipsIfLogExists()
  53. {
  54. $test = new TestHandler();
  55. $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
  56. $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
  57. $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
  58. $handler->flush();
  59. $this->assertFalse($test->hasErrorRecords());
  60. $this->assertFalse($test->hasCriticalRecords());
  61. $this->assertFalse($test->hasWarningRecords());
  62. }
  63. /**
  64. * @covers Monolog\Handler\DeduplicationHandler::flush
  65. * @covers Monolog\Handler\DeduplicationHandler::appendRecord
  66. * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
  67. * @depends testFlushPassthruIfEmptyLog
  68. */
  69. public function testFlushPassthruIfLogTooOld()
  70. {
  71. $test = new TestHandler();
  72. $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
  73. $record = $this->getRecord(Logger::ERROR);
  74. $record['datetime'] = $record['datetime']->modify('+62seconds');
  75. $handler->handle($record);
  76. $record = $this->getRecord(Logger::CRITICAL);
  77. $record['datetime'] = $record['datetime']->modify('+62seconds');
  78. $handler->handle($record);
  79. $handler->flush();
  80. $this->assertTrue($test->hasErrorRecords());
  81. $this->assertTrue($test->hasCriticalRecords());
  82. $this->assertFalse($test->hasWarningRecords());
  83. }
  84. /**
  85. * @covers Monolog\Handler\DeduplicationHandler::flush
  86. * @covers Monolog\Handler\DeduplicationHandler::appendRecord
  87. * @covers Monolog\Handler\DeduplicationHandler::isDuplicate
  88. * @covers Monolog\Handler\DeduplicationHandler::collectLogs
  89. */
  90. public function testGcOldLogs()
  91. {
  92. $test = new TestHandler();
  93. @unlink(sys_get_temp_dir().'/monolog_dedup.log');
  94. $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
  95. // handle two records from yesterday, and one recent
  96. $record = $this->getRecord(Logger::ERROR);
  97. $record['datetime'] = $record['datetime']->modify('-1day -10seconds');
  98. $handler->handle($record);
  99. $record2 = $this->getRecord(Logger::CRITICAL);
  100. $record2['datetime'] = $record2['datetime']->modify('-1day -10seconds');
  101. $handler->handle($record2);
  102. $record3 = $this->getRecord(Logger::CRITICAL);
  103. $record3['datetime'] = $record3['datetime']->modify('-30seconds');
  104. $handler->handle($record3);
  105. // log is written as none of them are duplicate
  106. $handler->flush();
  107. $this->assertSame(
  108. $record['datetime']->getTimestamp() . ":ERROR:test\n" .
  109. $record2['datetime']->getTimestamp() . ":CRITICAL:test\n" .
  110. $record3['datetime']->getTimestamp() . ":CRITICAL:test\n",
  111. file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
  112. );
  113. $this->assertTrue($test->hasErrorRecords());
  114. $this->assertTrue($test->hasCriticalRecords());
  115. $this->assertFalse($test->hasWarningRecords());
  116. // clear test handler
  117. $test->clear();
  118. $this->assertFalse($test->hasErrorRecords());
  119. $this->assertFalse($test->hasCriticalRecords());
  120. // log new records, duplicate log gets GC'd at the end of this flush call
  121. $handler->handle($record = $this->getRecord(Logger::ERROR));
  122. $handler->handle($record2 = $this->getRecord(Logger::CRITICAL));
  123. $handler->flush();
  124. // log should now contain the new errors and the previous one that was recent enough
  125. $this->assertSame(
  126. $record3['datetime']->getTimestamp() . ":CRITICAL:test\n" .
  127. $record['datetime']->getTimestamp() . ":ERROR:test\n" .
  128. $record2['datetime']->getTimestamp() . ":CRITICAL:test\n",
  129. file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
  130. );
  131. $this->assertTrue($test->hasErrorRecords());
  132. $this->assertTrue($test->hasCriticalRecords());
  133. $this->assertFalse($test->hasWarningRecords());
  134. }
  135. public static function tearDownAfterClass(): void
  136. {
  137. @unlink(sys_get_temp_dir().'/monolog_dedup.log');
  138. }
  139. }