improved mc logs folder cleaning

This commit is contained in:
Steffen Schröder 2014-06-20 16:51:08 +02:00
parent 51e2fde2b0
commit 4a122927dc
2 changed files with 34 additions and 11 deletions

View File

@ -161,30 +161,40 @@ abstract class FileUtil {
} }
/** /**
* Delete old ManiaControl Log Files * Clean the given directory by deleting old files
* *
* @param float $maxFileAgeInDays * @param string $directory
* @param float $maxFileAgeInDays
* @param bool $recursive
* @return bool * @return bool
*/ */
public static function deleteOldLogFiles($maxFileAgeInDays = 10.) { public static function cleanDirectory($directory, $maxFileAgeInDays = 10., $recursive = false) {
$logsFolderPath = Logger::getLogsFolder(); if (!is_dir($directory) || !is_readable($directory)) {
if (!is_readable($logsFolderPath)) {
return false; return false;
} }
$dirHandle = opendir($logsFolderPath); $dirHandle = opendir($directory);
if (!is_resource($dirHandle)) { if (!is_resource($dirHandle)) {
return false; return false;
} }
$time = time(); $time = time();
while ($fileName = readdir($dirHandle)) { while ($fileName = readdir($dirHandle)) {
$filePath = $logsFolderPath . $fileName; $filePath = $directory . $fileName;
if (!is_readable($filePath)) { if (!is_readable($filePath)) {
continue; continue;
} }
$fileModTime = filemtime($filePath); if (is_dir($filePath) && $recursive) {
$timeDeltaDays = ($time - $fileModTime) / (24. * 3600.); // Directory
if ($timeDeltaDays > $maxFileAgeInDays) { self::cleanDirectory($filePath . DIRECTORY_SEPARATOR, $maxFileAgeInDays, $recursive);
unlink($filePath); } else if (is_file($filePath)) {
// File
if (!is_writable($filePath)) {
continue;
}
$fileModTime = filemtime($filePath);
$timeDeltaDays = ($time - $fileModTime) / (24. * 3600.);
if ($timeDeltaDays > $maxFileAgeInDays) {
unlink($filePath);
}
} }
} }
closedir($dirHandle); closedir($dirHandle);

View File

@ -2,6 +2,8 @@
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Files\FileUtil;
/** /**
* ManiaControl Logger Class * ManiaControl Logger Class
* *
@ -16,6 +18,7 @@ class Logger {
*/ */
public static function setup() { public static function setup() {
self::setupErrorLogFileName(); self::setupErrorLogFileName();
self::cleanLogsFolder();
} }
/** /**
@ -54,6 +57,16 @@ class Logger {
return $logsFolder; return $logsFolder;
} }
/**
* Delete old ManiaControl Log Files
*
* @return bool
*/
private static function cleanLogsFolder() {
$logsFolderPath = self::getLogsFolder();
return FileUtil::cleanDirectory($logsFolderPath);
}
/** /**
* Log and echo the given Error Message * Log and echo the given Error Message
* *