normalizeDirectory($dirPath, $historyNumberOfDays); parent::__construct( $dirPath . date('Y-m-d') . '.log', $level, $bubble, $filePermission, $useLocking ); $this->setFormatter(new LogFileFormatter()); } /** * Normalize a directory: create if missing and clear old files * * @param string $dirPath The directory path * @param int $historyNumberOfDays The maximum number of history files to keep (in number of days) * * @return void * * @throws Throwable If an error occurs */ private function normalizeDirectory (string $dirPath, int $historyNumberOfDays = 30): void { if (!file_exists($dirPath) || !is_dir($dirPath)) { error_clear_last(); if (!mkdir($dirPath, 0755, true)) { throw new RuntimeException('Failed to create log directory: ' . $dirPath, 0, LastErrorException::createFromLastError()); } } $dateLimit = (new DateTimeImmutable('now'))->sub(new DateInterval('P' . $historyNumberOfDays . 'D')); $iterator = new DirectoryIterator($dirPath); foreach ($iterator as $fileInfo) { if ($fileInfo->isDot() || mb_substr($fileInfo->getFilename(), 0, 1) === '.') { continue; } if (!$fileInfo->isFile()) { continue; } if (preg_match('/^(?\\d{4}-\\d{2}-\\d{2})(?:[_-].*)?\.log$/i', $fileInfo->getFilename(), $match) !== 1) { continue; } $date = DateTimeImmutable::createFromFormat('Y-m-d', $match['date']); if ($date >= $dateLimit) { continue; } unlink($fileInfo->getPathname()); } } }