rand_function = function($min, $max) { return rand($min, $max); }; } else { $this->rand_function = $rand_function; } } protected function write(array $record) { $model = $this->createNewLogModel(); $model->hydrate([ 'name' => $record['channel'], 'level' => $record['level'], 'message' => $record['formatted'], 'created_at' => $record['datetime']->format('Y-m-d H:i:s'), ]); $model->save(); if ($this->getRandom() <= self::LOG_PURGE_PROBABILITY) { $this->purgeOldLogs(); } } private function createNewLogModel() { return Log::create(); } private function getRandom() { return call_user_func($this->rand_function, 0, 100); } private function purgeOldLogs() { Log::whereLt('created_at', Carbon::create()->subDays(self::DAYS_TO_KEEP_LOGS)->toDateTimeString()) ->deleteMany(); } }