123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- <?php
- if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
-
- require_once(LOG4PHP_DIR . '/appenders/LoggerAppenderFile.php');
- class LoggerAppenderRollingFile extends LoggerAppenderFile {
-
- var $maxFileSize = 10485760;
-
-
- var $maxBackupIndex = 1;
-
-
- var $expandedFileName = null;
-
- function LoggerAppenderRollingFile($name)
- {
- $this->LoggerAppenderFile($name);
- }
-
-
- function getExpandedFileName() {
- return $this->expandedFileName;
- }
-
- function getMaxBackupIndex() {
- return $this->maxBackupIndex;
- }
-
- function getMaximumFileSize() {
- return $this->maxFileSize;
- }
-
- function rollOver()
- {
-
- if($this->maxBackupIndex > 0) {
- $fileName = $this->getExpandedFileName();
-
- $file = $fileName . '.' . $this->maxBackupIndex;
- if (is_writable($file))
- unlink($file);
-
- for ($i = $this->maxBackupIndex - 1; $i >= 1; $i--) {
- $file = $fileName . "." . $i;
- if (is_readable($file)) {
- $target = $fileName . '.' . ($i + 1);
- rename($file, $target);
- }
- }
-
-
- $target = $fileName . ".1";
-
- $this->closeFile();
-
- $file = $fileName;
- rename($file, $target);
- }
-
- $this->setFile($fileName, false);
- unset($this->fp);
- $this->activateOptions();
- }
-
- function setFileName($fileName)
- {
- $this->fileName = $fileName;
- $this->expandedFileName = realpath($fileName);
- LoggerLog::debug("LoggerAppenderRollingFile::setFileName():filename=[{$fileName}]:expandedFileName=[{$this->expandedFileName}]");
- }
-
- function setMaxBackupIndex($maxBackups)
- {
- if (is_numeric($maxBackups))
- $this->maxBackupIndex = abs((int)$maxBackups);
- }
-
- function setMaximumFileSize($maxFileSize)
- {
- $this->setMaxFileSize($maxFileSize);
- }
-
- function setMaxFileSize($value)
- {
- $maxFileSize = null;
- $numpart = substr($value,0, strlen($value) -2);
- $suffix = strtoupper(substr($value, -2));
- switch ($suffix) {
- case 'KB': $maxFileSize = (int)((int)$numpart * 1024); break;
- case 'MB': $maxFileSize = (int)((int)$numpart * 1024 * 1024); break;
- case 'GB': $maxFileSize = (int)((int)$numpart * 1024 * 1024 * 1024); break;
- default:
- if (is_numeric($value)) {
- $maxFileSize = (int)$value;
- }
- }
-
- if ($maxFileSize === null) {
- LoggerLog::debug("LoggerAppenderRollingFile::setMaxFileSize():value=[$value] wrong declaration");
- } else {
- $this->maxFileSize = abs($maxFileSize);
- }
- }
-
- function append($event)
- {
- if ($this->fp) {
- parent::append($event);
- if (ftell($this->fp) > $this->getMaximumFileSize())
- $this->rollOver();
- }
- }
- }
- ?>
|