Kaynağa Gözat

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

* feat: RotatingHandler Capable of handling log files in nested directory format
Chuoke 2 ay önce
ebeveyn
işleme
78e6df032e
1 değiştirilmiş dosya ile 13 ekleme ve 0 silme
  1. 13 0
      src/Monolog/Handler/RotatingFileHandler.php

+ 13 - 0
src/Monolog/Handler/RotatingFileHandler.php

@@ -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();
             }
         }