ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
DeduplicationHandlerTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
15 use Monolog\Logger;
16 
18 {
23  {
24  $test = new TestHandler();
25  @unlink(sys_get_temp_dir().'/monolog_dedup.log');
26  $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
27 
28  $handler->handle($this->getRecord(Logger::DEBUG));
29  $handler->handle($this->getRecord(Logger::INFO));
30 
31  $handler->flush();
32 
33  $this->assertTrue($test->hasInfoRecords());
34  $this->assertTrue($test->hasDebugRecords());
35  $this->assertFalse($test->hasWarningRecords());
36  }
37 
42  public function testFlushPassthruIfEmptyLog()
43  {
44  $test = new TestHandler();
45  @unlink(sys_get_temp_dir().'/monolog_dedup.log');
46  $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
47 
48  $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
49  $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
50 
51  $handler->flush();
52 
53  $this->assertTrue($test->hasErrorRecords());
54  $this->assertTrue($test->hasCriticalRecords());
55  $this->assertFalse($test->hasWarningRecords());
56  }
57 
64  public function testFlushSkipsIfLogExists()
65  {
66  $test = new TestHandler();
67  $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
68 
69  $handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
70  $handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
71 
72  $handler->flush();
73 
74  $this->assertFalse($test->hasErrorRecords());
75  $this->assertFalse($test->hasCriticalRecords());
76  $this->assertFalse($test->hasWarningRecords());
77  }
78 
85  public function testFlushPassthruIfLogTooOld()
86  {
87  $test = new TestHandler();
88  $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
89 
90  $record = $this->getRecord(Logger::ERROR);
91  $record['datetime']->modify('+62seconds');
92  $handler->handle($record);
93  $record = $this->getRecord(Logger::CRITICAL);
94  $record['datetime']->modify('+62seconds');
95  $handler->handle($record);
96 
97  $handler->flush();
98 
99  $this->assertTrue($test->hasErrorRecords());
100  $this->assertTrue($test->hasCriticalRecords());
101  $this->assertFalse($test->hasWarningRecords());
102  }
103 
110  public function testGcOldLogs()
111  {
112  $test = new TestHandler();
113  @unlink(sys_get_temp_dir().'/monolog_dedup.log');
114  $handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
115 
116  // handle two records from yesterday, and one recent
117  $record = $this->getRecord(Logger::ERROR);
118  $record['datetime']->modify('-1day -10seconds');
119  $handler->handle($record);
120  $record2 = $this->getRecord(Logger::CRITICAL);
121  $record2['datetime']->modify('-1day -10seconds');
122  $handler->handle($record2);
123  $record3 = $this->getRecord(Logger::CRITICAL);
124  $record3['datetime']->modify('-30seconds');
125  $handler->handle($record3);
126 
127  // log is written as none of them are duplicate
128  $handler->flush();
129  $this->assertSame(
130  $record['datetime']->getTimestamp() . ":ERROR:test\n" .
131  $record2['datetime']->getTimestamp() . ":CRITICAL:test\n" .
132  $record3['datetime']->getTimestamp() . ":CRITICAL:test\n",
133  file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
134  );
135  $this->assertTrue($test->hasErrorRecords());
136  $this->assertTrue($test->hasCriticalRecords());
137  $this->assertFalse($test->hasWarningRecords());
138 
139  // clear test handler
140  $test->clear();
141  $this->assertFalse($test->hasErrorRecords());
142  $this->assertFalse($test->hasCriticalRecords());
143 
144  // log new records, duplicate log gets GC'd at the end of this flush call
145  $handler->handle($record = $this->getRecord(Logger::ERROR));
146  $handler->handle($record2 = $this->getRecord(Logger::CRITICAL));
147  $handler->flush();
148 
149  // log should now contain the new errors and the previous one that was recent enough
150  $this->assertSame(
151  $record3['datetime']->getTimestamp() . ":CRITICAL:test\n" .
152  $record['datetime']->getTimestamp() . ":ERROR:test\n" .
153  $record2['datetime']->getTimestamp() . ":CRITICAL:test\n",
154  file_get_contents(sys_get_temp_dir() . '/monolog_dedup.log')
155  );
156  $this->assertTrue($test->hasErrorRecords());
157  $this->assertTrue($test->hasCriticalRecords());
158  $this->assertFalse($test->hasWarningRecords());
159  }
160 
161  public static function tearDownAfterClass()
162  {
163  @unlink(sys_get_temp_dir().'/monolog_dedup.log');
164  }
165 }
const DEBUG
Detailed debug information.
Definition: Logger.php:33
testFlushPassthruIfLogTooOld()
Monolog::flush Monolog::appendRecord Monolog::isDuplicate testFlushPassthruIfEmptyLog ...
const ERROR
Runtime errors.
Definition: Logger.php:58
getRecord($level=Logger::WARNING, $message='test', $context=array())
Definition: TestCase.php:19
testFlushPassthruIfEmptyLog()
Monolog::flush Monolog::appendRecord
Simple handler wrapper that deduplicates log records across multiple requests.
testFlushSkipsIfLogExists()
Monolog::flush Monolog::appendRecord Monolog::isDuplicate testFlushPassthruIfEmptyLog ...
const CRITICAL
Critical conditions.
Definition: Logger.php:65
Used for testing purposes.
Definition: TestHandler.php:66
testGcOldLogs()
Monolog::flush Monolog::appendRecord Monolog::isDuplicate Monolog::collectLogs ...
$handler
$test
Definition: Utf8Test.php:84
const INFO
Interesting events.
Definition: Logger.php:40