randFunction = $randFunction; $this->logRepository = $logRepository; } protected function write(array $record) { $entity = new LogEntity(); $entity->setName($record['channel']); $entity->setLevel($record['level']); $entity->setMessage($record['formatted']); $entity->setCreatedAt($record['datetime']); $this->logRepository->persist($entity); $this->logRepository->flush(); 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() { Log::whereLt('created_at', Carbon::now()->subDays(self::DAYS_TO_KEEP_LOGS)->toDateTimeString()) ->deleteMany(); } }