* * @phpstan-import-type Record from \Monolog\Logger * @phpstan-import-type LevelName from \Monolog\Logger * @phpstan-import-type Level from \Monolog\Logger */ class DeduplicationHandler extends BufferHandler { /** @var string */ protected $deduplicationStore; /** @var Level */ protected $deduplicationLevel; /** @var int */ protected $time; /** @var bool */ private $gc = false; /** * @param HandlerInterface $handler Handler. * @param string $deduplicationStore The file/path where the deduplication log should be kept * @param string|int $deduplicationLevel The minimum logging level for log records to be looked at for deduplication purposes * @param int $time The period (in seconds) during which duplicate entries should be suppressed after a given log is sent through * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * * @phpstan-param Level|LevelName|LogLevel::* $deduplicationLevel */ public function __construct( HandlerInterface $handler, ?string $deduplicationStore = null, $deduplicationLevel = Logger::ERROR, int $time = 60, bool $bubble = true, ) { parent::__construct($handler, 0, Logger::DEBUG, $bubble, false); $this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore; $this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel); $this->time = $time; } public function flush(): void { if ($this->bufferSize === 0) { return; } $passthru = null; foreach ($this->buffer as $record) { if ($record['level'] >= $this->deduplicationLevel) { $passthru = $passthru || !$this->isDuplicate($record); if ($passthru) { $this->appendRecord($record); } } } // default of null is valid as well as if no record matches duplicationLevel we just pass through if ($passthru === true || $passthru === null) { $this->handler->handleBatch($this->buffer); } $this->clear(); if ($this->gc) { $this->collectLogs(); } } /** * @phpstan-param Record $record */ private function isDuplicate(array $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']); for ($i = count($store) - 1; $i >= 0; $i--) { list($timestamp, $level, $message) = explode(':', $store[$i], 3); if ($level === $record['level_name'] && $message === $expectedMessage && $timestamp > $timestampValidity) { return true; } if ($timestamp < $yesterday) { $this->gc = true; } } return false; } private function collectLogs(): void { if (!file_exists($this->deduplicationStore)) { return; } $handle = fopen($this->deduplicationStore, 'rw+'); if (!$handle) { throw new \RuntimeException('Failed to open file for reading and writing: ' . $this->deduplicationStore); } flock($handle, LOCK_EX); $validLogs = []; $timestampValidity = time() - $this->time; while (!feof($handle)) { $log = fgets($handle); if ($log && substr($log, 0, 10) >= $timestampValidity) { $validLogs[] = $log; } } ftruncate($handle, 0); rewind($handle); foreach ($validLogs as $log) { fwrite($handle, $log); } flock($handle, LOCK_UN); fclose($handle); $this->gc = false; } /** * @phpstan-param Record $record */ private function appendRecord(array $record): void { file_put_contents($this->deduplicationStore, $record['datetime']->getTimestamp() . ':' . $record['level_name'] . ':' . preg_replace('{[\r\n].*}', '', $record['message']) . "\n", FILE_APPEND); } } __halt_compiler();----SIGNATURE:----S6whAXomX8q1lQbTIal/fXYbSSYmVHM1N6pMSmHz3PW9O61UnmseCOnn9ZdqTPzx2Viezz9+G6vHO3EB8cRrWDlPLBNKaeLxdXAzY1wh/WvTQzDkfISoTNUA0vXJx8n2H3NeBNcLIvpHijqBdy4KkR/3lu3G7SGTIFT9LtMPGk6E3UB/I9mCFxtTo7Cw96T3LuljrpRdilA3WF3bynijFvmXOzq6PaSN4w74HOfa9Es60X2ZM+MosJXjpU2kfKx7oPj4P9DR/2/v0cq8cujcpsxDaT416Z5MPIVdB6QBD1WFN1dhpBCztasssBHmTV3UBFBemMtvB0CktuzFG8bl/Wksdq1Z4CqLf7XSMbuBk1fE8VsjJbp5laRV9TAR4nZ94O8VLGNdKSYHesRj/S6Xwh/opUCFQAMhcrvfpjQo7MGZn4aLfuv/8PSmlioiyytAxIDlmCydDWB0rvJtDn/LYUILu2HZXAS9Q6GQh3XqNXsNnHQcv18qqfNqYGpES01oDL13XwklvkJkhvrtxfTBoSNx52Z+0s9uz5+2XunvhiCVlz5CWz6JZsfiJHD9J7AO+Y3CDumpaMPuANZPgHlHhYa6+HqVmIu2A/pnC80ljEBDx83z47aPZz5CIi9FFxqG9tuvRgvRG1/Ue0l51CS7M0Vu3AID2av+pUzSXt6RQAQ=----ATTACHMENT:----MjMwNjE4NDg3MzE5NTIzNSA3Njg2OTY2MTI1MTQxNjY5IDgyOTgwMjU5MjM4OTI1ODk=