aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Kevin Papst <kpapst@gmx.net> 2017-12-08 17:08:41 +0100
committerGravatar Kevin Papst <kpapst@gmx.net> 2017-12-08 17:08:41 +0100
commita96b751d319665c6702bcf60feffdcf56694003a (patch)
treeb3cf8a78f368d01771cffd12941224b2d5cd51b4 /lib
parent3902d3f43330504945e78627b4c49e67ae88aea9 (diff)
make sure that we do not exceed a certain file size for the users log file
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Log.php22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php
index 9559a0bd4..72947272e 100644
--- a/lib/Minz/Log.php
+++ b/lib/Minz/Log.php
@@ -20,6 +20,8 @@ class Minz_Log {
const NOTICE = 8;
const DEBUG = 16;
+ const MAX_LOG_SIZE = 512000; // 500kB
+
/**
* Enregistre un message dans un fichier de log spécifique
* Message non loggué si
@@ -29,6 +31,7 @@ class Minz_Log {
* @param $information message d'erreur / information à enregistrer
* @param $level niveau d'erreur
* @param $file_name fichier de log
+ * @throws Minz_PermissionDeniedException
*/
public static function record ($information, $level, $file_name = null) {
try {
@@ -70,6 +73,8 @@ class Minz_Log {
. ' [' . $level_label . ']'
. ' --- ' . $information . "\n";
+ self::checkForLogfileSize($file_name);
+
if (file_put_contents($file_name, $log, FILE_APPEND | LOCK_EX) === false) {
throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
}
@@ -77,6 +82,23 @@ class Minz_Log {
}
/**
+ * Make sure we do not waste a huge amount of disk space with old log messages.
+ *
+ * This method can be called multiple times for one script execution, but its result will not change unless
+ * you call clearstatcache() in between. We won't due do that for performance reasons.
+ *
+ * @param $file_name
+ * @throws Minz_PermissionDeniedException
+ */
+ protected static function checkForLogfileSize($file_name) {
+ if (file_exists($file_name) && filesize($file_name) > self::MAX_LOG_SIZE) {
+ if (!unlink($file_name)) {
+ throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
+ }
+ }
+ }
+
+ /**
* Automatise le log des variables globales $_GET et $_POST
* Fait appel à la fonction record(...)
* Ne fonctionne qu'en environnement "development"