diff options
| author | 2017-12-10 21:31:41 +0100 | |
|---|---|---|
| committer | 2017-12-10 21:58:24 +0100 | |
| commit | b1c317a253445a6458f1263c1b622a788cc7cd0e (patch) | |
| tree | d348ddbc62a98e53b50ca29cba352640b1aeb54d /lib/Minz/Log.php | |
| parent | 452886ea3ac4b91bc72952df659fb53ae7807c22 (diff) | |
Log rotation, use Minz_Log, new log constants
ADMIN_LOG, API_LOG, PSHB_LOG
Diffstat (limited to 'lib/Minz/Log.php')
| -rw-r--r-- | lib/Minz/Log.php | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/lib/Minz/Log.php b/lib/Minz/Log.php index 6231754fa..5e7831cdb 100644 --- a/lib/Minz/Log.php +++ b/lib/Minz/Log.php @@ -71,7 +71,7 @@ class Minz_Log { . ' [' . $level_label . ']' . ' --- ' . $information . "\n"; - self::checkLogfileSize($file_name); + 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); @@ -88,12 +88,24 @@ class Minz_Log { * @param $file_name * @throws Minz_PermissionDeniedException */ - protected static function checkLogfileSize($file_name) { - $maxSize = defined('MAX_LOG_SIZE') ? MAX_LOG_SIZE : 512000; - if (@filesize($file_name) > $maxSize) { - if (file_put_contents($file_name, '') === false) { + 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); + } } } |
