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
*/
public static function deleteOldLogFiles($maxFileAgeInDays = 10.) {
$logsFolderPath = Logger::getLogsFolder();
if (!is_readable($logsFolderPath)) {
public static function cleanDirectory($directory, $maxFileAgeInDays = 10., $recursive = false) {
if (!is_dir($directory) || !is_readable($directory)) {
return false;
}
$dirHandle = opendir($logsFolderPath);
$dirHandle = opendir($directory);
if (!is_resource($dirHandle)) {
return false;
}
$time = time();
while ($fileName = readdir($dirHandle)) {
$filePath = $logsFolderPath . $fileName;
$filePath = $directory . $fileName;
if (!is_readable($filePath)) {
continue;
}
$fileModTime = filemtime($filePath);
$timeDeltaDays = ($time - $fileModTime) / (24. * 3600.);
if ($timeDeltaDays > $maxFileAgeInDays) {
unlink($filePath);
if (is_dir($filePath) && $recursive) {
// Directory
self::cleanDirectory($filePath . DIRECTORY_SEPARATOR, $maxFileAgeInDays, $recursive);
} 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);

View File

@ -2,6 +2,8 @@
namespace ManiaControl;
use ManiaControl\Files\FileUtil;
/**
* ManiaControl Logger Class
*
@ -16,6 +18,7 @@ class Logger {
*/
public static function setup() {
self::setupErrorLogFileName();
self::cleanLogsFolder();
}
/**
@ -54,6 +57,16 @@ class Logger {
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
*