improved log fodder creation

deleting of old log files
This commit is contained in:
Steffen Schröder 2014-06-20 14:57:02 +02:00
parent 53e3078cc1
commit 6e08ee9ac7
2 changed files with 87 additions and 23 deletions

View File

@ -14,6 +14,7 @@ use ManiaControl\Utils\Formatter;
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
abstract class FileUtil { abstract class FileUtil {
/** /**
* Load a remote file * Load a remote file
* *
@ -186,4 +187,35 @@ abstract class FileUtil {
} }
} }
} }
/**
* Delete old ManiaControl Log Files
*
* @param int $maxFileAgeInDays
* @return bool
*/
public static function deleteOldLogFiles($maxFileAgeInDays = 14) {
$logsFolderPath = Logger::getLogsFolderPath();
if (!is_readable($logsFolderPath)) {
return false;
}
$dirHandle = opendir($logsFolderPath);
if (!is_resource($dirHandle)) {
return false;
}
$time = time();
while ($fileName = readdir($dirHandle)) {
$filePath = $logsFolderPath . $fileName;
if (!is_readable($filePath)) {
continue;
}
$fileModTime = filemtime($filePath);
$timeDeltaDays = ($time - $fileModTime) / (24 * 3600);
if ($timeDeltaDays > $maxFileAgeInDays) {
unlink($filePath);
}
}
closedir($dirHandle);
return true;
}
} }

View File

@ -2,6 +2,8 @@
namespace ManiaControl; namespace ManiaControl;
use ManiaControl\Files\FileUtil;
/** /**
* ManiaControl Logger Class * ManiaControl Logger Class
* *
@ -15,19 +17,62 @@ class Logger {
* Setup the Logging Mechanism * Setup the Logging Mechanism
*/ */
public static function setup() { public static function setup() {
$logFileName = ManiaControlDir . 'logs' . DIRECTORY_SEPARATOR; self::setupErrorLogFileName();
if (!is_dir($logFileName) && !mkdir($logFileName)) { FileUtil::deleteOldLogFiles();
echo "Couldn't create Logs Folder, please check the File Permissions!"; }
/**
* Set the Error Log File Name
*/
private function setupErrorLogFileName() {
$logsFolder = self::createLogsFolder();
if ($logsFolder) {
$logFileName = $logsFolder . 'ManiaControl';
if (LOG_NAME_USE_DATE) {
$logFileName .= '_' . date('Y-m-d');
}
if (LOG_NAME_USE_PID) {
$logFileName .= '_' . getmypid();
}
$logFileName .= '.log';
ini_set('error_log', $logFileName);
} }
$logFileName .= 'ManiaControl'; }
if (LOG_NAME_USE_DATE) {
$logFileName .= '_' . date('Y-m-d'); /**
* Create the Logs Folder and return its Path if successful
*
* @return bool|string
*/
private static function createLogsFolder() {
$logsFolderPath = self::getLogsFolderPath();
if (!is_dir($logsFolderPath) && !mkdir($logsFolderPath)) {
self::output("Couldn't create Logs Folder, please check the File Permissions!");
return false;
} }
if (LOG_NAME_USE_PID) { return $logsFolderPath;
$logFileName .= '_' . getmypid(); }
/**
* Build the Logs Folder Path
*
* @return string
*/
public static function getLogsFolderPath() {
return ManiaControlDir . 'logs' . DIRECTORY_SEPARATOR;
}
/**
* Echo the given Message
*
* @param string $message
* @param bool $eol
*/
public static function output($message, $eol = true) {
if ($eol) {
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
} }
$logFileName .= '.log'; echo $message;
ini_set('error_log', $logFileName);
} }
/** /**
@ -56,19 +101,6 @@ class Logger {
} }
} }
/**
* Echo the given Message
*
* @param string $message
* @param bool $eol
*/
public static function output($message, $eol = true) {
if ($eol) {
$message = '[' . date('d-M-Y H:i:s e') . '] ' . $message . PHP_EOL;
}
echo $message;
}
/** /**
* Log and echo the given Info Message * Log and echo the given Info Message
* *