summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2017-12-11 19:58:09 +0100
committerGravatar GitHub <noreply@github.com> 2017-12-11 19:58:09 +0100
commit27c3092da42854772100101b3ba9a7507c86f3a1 (patch)
treef52125dd6429e0c2aee2112804feebf2e5f7b08d /lib
parentc722b2cdb7a23ddd37e4cee8b612d4896139038d (diff)
parent3ddbff6bd91ae16edfe2259ebd1bea04c86c7601 (diff)
Merge pull request #1712 from kevinpapst/logfilesize
Prevent logfile from growing unlimited
Diffstat (limited to 'lib')
-rw-r--r--lib/Minz/Log.php33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php
index 9559a0bd4..5e7831cdb 100644
--- a/lib/Minz/Log.php
+++ b/lib/Minz/Log.php
@@ -29,6 +29,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 +71,8 @@ class Minz_Log {
. ' [' . $level_label . ']'
. ' --- ' . $information . "\n";
+ self::ensureMaxLogSize($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 +80,36 @@ 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 ensureMaxLogSize($file_name) {
+ $maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 1048576;
+ if ($maxSize > 0 && @filesize($file_name) > $maxSize) {
+ $fp = fopen($file_name, 'c+');
+ if ($fp && flock($fp, LOCK_EX)) {
+ fseek($fp, -intval($maxSize / 2), SEEK_END);
+ $content = fread($fp, $maxSize);
+ rewind($fp);
+ ftruncate($fp, 0);
+ fwrite($fp, $content ? $content : '');
+ fflush($fp);
+ flock($fp, LOCK_UN);
+ } else {
+ throw new Minz_PermissionDeniedException($file_name, Minz_Exception::ERROR);
+ }
+ if ($fp) {
+ fclose($fp);
+ }
+ }
+ }
+
+ /**
* Automatise le log des variables globales $_GET et $_POST
* Fait appel à la fonction record(...)
* Ne fonctionne qu'en environnement "development"