1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-28 19:14:04 +01:00

feat: Add automatic empty directory cleanup in RotatingFileHandler (#2000)

* feat: RotatingHandler Capable of handling log files in nested directory format
This commit is contained in:
Chuoke
2025-10-24 16:29:18 +08:00
committed by GitHub
parent efab5aabaf
commit 78e6df032e

View File

@@ -146,6 +146,8 @@ class RotatingFileHandler extends StreamHandler
return strcmp($b, $a);
});
$basePath = dirname($this->filename);
foreach (\array_slice($logFiles, $this->maxFiles) as $file) {
if (is_writable($file)) {
// suppress errors here as unlink() might fail if two processes
@@ -154,6 +156,17 @@ class RotatingFileHandler extends StreamHandler
return true;
});
unlink($file);
$dir = dirname($file);
while ($dir !== $basePath) {
$entries = scandir($dir);
if ($entries === false || \count(array_diff($entries, ['.', '..'])) > 0) {
break;
}
rmdir($dir);
$dir = dirname($dir);
}
restore_error_handler();
}
}