* * @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:----unu85zxOtIzrNoChYULWS7rPJgaa2oT0xIQqFiUP06V0cMDSdlWs0WVk+GtB8zmBCOSuUfWnGQ24lG5PRRJQ5IeZ9gbMuJm+p2OtzUFB1DUSKHGoup7fs7d32hN4oJddlGGl3zV3EL1EJFzGcDuK6Ctd/ofYgufjyH/69XW+4m/J9ua/t7vhd8anY0sfass4xrsUoOWLXwo+2Q5z3YOqHBf4LAfYG+9mDuydv+YU1GdoJYtj4596mEuJ9x0eKKFL/S5le2L2ueA3dfDnMQpBVsophng/0b8Kod6sqLpB85ONv8iQ55kmMUd/gVn4GbYAoInl/il4NeYAZoyM+2SmbMWZ1ZMEQPQ2VBE7PqUYzqhu2A6v0M3MV4FwwiwTanJb6k87SawGVm3nCaLefzcSKMncwrQg10P92t5ZyGVK7HgjTtCWdF28PmRLiHeY5lVXQR96VS2RUoIWw99urXQfJcuZ0et/TF543vrof/jgjxr39DYu1WNQQS/8moMyalOBHaMWD+39lr+epYOYWhI5858x5RY0PlwOB3XR5zl0+Lmo8mV8nItA0on4V4W5gBznLMl3JzRuazHre4VfbLXThNvapfLARKgZLn/WXmi3Byy4t89xCQN1JU90CcJt5je2Us6LVJNI1NiVRpaolKL/ejF2wrfw0NOefC09xDJO8aQ=----ATTACHMENT:----NzE0Mjg5ODc4NjE5ODMyIDQyMjgxNDg5MDI3MTczMTEgMzY1ODM1MTE0NzA3MjU0NA==