Skip to content

Commit f56f296

Browse files
committed
Fix handling of messages with new lines
1 parent 0e3a4bf commit f56f296

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/Monolog/Handler/DeduplicationHandler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,12 @@ private function isDuplicate(array $record)
114114

115115
$yesterday = time() - 86400;
116116
$timestampValidity = $record['datetime']->getTimestamp() - $this->time;
117-
$line = $record['level_name'] . ':' . $record['message'];
117+
$expectedMessage = preg_replace('{[\r\n].*}', '', $record['message']);
118118

119119
for ($i = count($store) - 1; $i >= 0; $i--) {
120120
list($timestamp, $level, $message) = explode(':', $store[$i], 3);
121121

122-
if ($level === $record['level_name'] && $message === $record['message'] && $timestamp > $timestampValidity) {
122+
if ($level === $record['level_name'] && $message === $expectedMessage && $timestamp > $timestampValidity) {
123123
return true;
124124
}
125125

@@ -164,6 +164,6 @@ private function collectLogs()
164164

165165
private function appendRecord(array $record)
166166
{
167-
file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . $record['message'] . "\n", FILE_APPEND);
167+
file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . preg_replace('{[\r\n].*}', '', $record['message']) . "\n", FILE_APPEND);
168168
}
169169
}

tests/Monolog/Handler/DeduplicationHandlerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testFlushPassthruIfEmptyLog()
4646
$handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
4747

4848
$handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
49-
$handler->handle($this->getRecord(Logger::CRITICAL));
49+
$handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
5050

5151
$handler->flush();
5252

@@ -67,7 +67,7 @@ public function testFlushSkipsIfLogExists()
6767
$handler = new DeduplicationHandler($test, sys_get_temp_dir().'/monolog_dedup.log', 0);
6868

6969
$handler->handle($this->getRecord(Logger::ERROR, 'Foo:bar'));
70-
$handler->handle($this->getRecord(Logger::CRITICAL));
70+
$handler->handle($this->getRecord(Logger::CRITICAL, "Foo\nbar"));
7171

7272
$handler->flush();
7373

0 commit comments

Comments
 (0)