diff options
| author | 2017-12-17 20:28:04 +0100 | |
|---|---|---|
| committer | 2017-12-17 20:28:04 +0100 | |
| commit | 60f56539c3f30fd3f7ba4f2a3570f7029ac93e5f (patch) | |
| tree | 1e78bfac7042dceb63898e2215db8fb0c1d7745d /lib/Minz/Log.php | |
| parent | ceda55c75b158fc1cf4813fe0f258527754b9289 (diff) | |
| parent | 0b1516af91792f86868689392f72ad4b6e32cdcf (diff) | |
Merge pull request #1720 from FreshRSS/dev
FreshRSS 1.9.0
Diffstat (limited to 'lib/Minz/Log.php')
| -rw-r--r-- | lib/Minz/Log.php | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 9559a0bd4..a8dbf8350 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,37 @@ 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 : ''); + fwrite($fp, sprintf("[%s] [notice] --- Log rotate.\n", date('r'))); + 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" |
