randFunction = $randFunction; $this->logRepository = $logRepository; } protected function write(array $record): void { $message = is_string($record['formatted']) ? $record['formatted'] : null; $entity = new LogEntity(); $entity->setName($record['channel']); $entity->setLevel((int)$record['level']); $entity->setMessage($message); $entity->setCreatedAt($record['datetime']); $entity->setRawMessage($record['message']); $entity->setContext($record['context']); $this->logRepository->saveLog($entity); if ($this->getRandom() <= self::LOG_PURGE_PROBABILITY) { $this->purgeOldLogs(); } } private function getRandom() { if ($this->randFunction) { return call_user_func($this->randFunction, 0, 100); } return rand(0, 100); } private function purgeOldLogs() { $this->logRepository->purgeOldLogs(self::DAYS_TO_KEEP_LOGS, self::PURGE_LIMIT); } }