Skip to content

Commit 78e6df0

Browse files
authored
feat: Add automatic empty directory cleanup in RotatingFileHandler (#2000)
* feat: RotatingHandler Capable of handling log files in nested directory format
1 parent efab5aa commit 78e6df0

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/Monolog/Handler/RotatingFileHandler.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ protected function rotate(): void
146146
return strcmp($b, $a);
147147
});
148148

149+
$basePath = dirname($this->filename);
150+
149151
foreach (\array_slice($logFiles, $this->maxFiles) as $file) {
150152
if (is_writable($file)) {
151153
// suppress errors here as unlink() might fail if two processes
@@ -154,6 +156,17 @@ protected function rotate(): void
154156
return true;
155157
});
156158
unlink($file);
159+
160+
$dir = dirname($file);
161+
while ($dir !== $basePath) {
162+
$entries = scandir($dir);
163+
if ($entries === false || \count(array_diff($entries, ['.', '..'])) > 0) {
164+
break;
165+
}
166+
167+
rmdir($dir);
168+
$dir = dirname($dir);
169+
}
157170
restore_error_handler();
158171
}
159172
}

0 commit comments

Comments
 (0)