8000 [#1878] User definable duplication for DeduplicationHandler by cracksalad · Pull Request #1879 · Seldaek/monolog · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[#1878] User definable duplication for DeduplicationHandler #1879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/Monolog/Handler/DeduplicationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class DeduplicationHandler extends BufferHandler
protected Level $deduplicationLevel;

protected int $time;

private bool $gc = false;
protected bool $gc = false;

/**
* @param HandlerInterface $handler Handler.
Expand All @@ -70,13 +69,24 @@ public function flush(): void
return;
}

$store = null;

if (file_exists($this->deduplicationStore)) {
$store = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}

$passthru = null;

foreach ($this->buffer as $record) {
if ($record->level->value >= $this->deduplicationLevel->value) {
$passthru = $passthru === true || !$this->isDuplicate($record);
$passthru = $passthru === true || !is_array($store) || !$this->isDuplicate($store, $record);
if ($passthru) {
$this->appendRecord($record);
$line = $this->buildDeduplicationStoreEntry($record);
file_put_contents($this->deduplicationStore, $line . "\n", FILE_APPEND);
if (!is_array($store)) {
$store = [];
}
$store[] = $line;
}
}
}
Expand All @@ -93,20 +103,15 @@ public function flush(): void
}
}

private function isDuplicate(LogRecord $record): bool
/**
* If there is a store entry older than e.g. a day, this method should set `$this->gc` to `true` to trigger garbage collection.
* @param string[] $store The deduplication store
*/
protected function isDuplicate(array $store, LogRecord $record): bool
{
if (!file_exists($this->deduplicationStore)) {
return false;
}

$store = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!is_array($store)) {
return false;
}

$yesterday = time() - 86400;
$timestampValidity = $record->datetime->getTimestamp() - $this->time;
$expectedMessage = preg_replace('{[\r\n].*}', '', $record->message);
$yesterday = time() - 86400;

for ($i = count($store) - 1; $i >= 0; $i--) {
list($timestamp, $level, $message) = explode(':', $store[$i], 3);
Expand All @@ -123,6 +128,14 @@ private function isDuplicate(LogRecord $record): bool
return false;
}

/**
* @return string The given record serialized as a single line of text
*/
protected function buildDeduplicationStoreEntry(LogRecord $record): string
{
return $record->datetime->getTimestamp() . ':' . $record->level->getName() . ':' . preg_replace('{[\r\n].*}', '', $record->message);
}

private function collectLogs(): void
{
if (!file_exists($this->deduplicationStore)) {
Expand Down Expand Up @@ -158,9 +171,4 @@ private function collectLogs(): void

$this->gc = false;
}

private function appendRecord(LogRecord $record): void
{
file_put_contents($this->deduplicationStore, $record->datetime->getTimestamp() . ':' . $record->level->getName() . ':' . preg_replace('{[\r\n].*}', '', $record->message) . "\n", FILE_APPEND);
}
}
0