Browse Source

w

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
54e36b75b3
  1. 10
      framework/YiiBase.php
  2. 168
      framework/logging/FileTarget.php

10
framework/YiiBase.php

@ -160,8 +160,8 @@ class YiiBase
* In the latter case, the root alias will be replaced by the corresponding registered path * In the latter case, the root alias will be replaced by the corresponding registered path
* and the remaining part will be appended to it. * and the remaining part will be appended to it.
* *
* In case the given alias is not an alias (i.e., not starting with '@'), * In case the given parameter is not an alias (i.e., not starting with '@'),
* it will be returned back as is. * it will be returned back without change.
* *
* Note, this method does not ensure the existence of the resulting path. * Note, this method does not ensure the existence of the resulting path.
* @param string $alias alias * @param string $alias alias
@ -173,14 +173,14 @@ class YiiBase
if (isset(self::$aliases[$alias])) { if (isset(self::$aliases[$alias])) {
return self::$aliases[$alias]; return self::$aliases[$alias];
} }
elseif ($alias[0] !== '@') { // not an alias
return $alias;
}
elseif (($pos = strpos($alias, '/')) !== false) { elseif (($pos = strpos($alias, '/')) !== false) {
$rootAlias = substr($alias, 0, $pos); $rootAlias = substr($alias, 0, $pos);
if (isset(self::$aliases[$rootAlias])) { if (isset(self::$aliases[$rootAlias])) {
return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos); return self::$aliases[$alias] = self::$aliases[$rootAlias] . substr($alias, $pos);
} }
else if($alias[0] !== '@') { // not an alias
return $alias;
}
} }
return false; return false;
} }

168
framework/logging/FileTarget.php

@ -1,47 +1,45 @@
<?php <?php
/** /**
* CFileLogRoute class file. * FileTarget class file.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/ * @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC * @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/ * @license http://www.yiiframework.com/license/
*/ */
/** /**
* CFileLogRoute records log messages in files. * FileTarget records log messages in files.
* *
* The log files are stored under {@link setLogPath logPath} and the file name * The log files are stored under [[logPath]] and their name
* is specified by {@link setLogFile logFile}. If the size of the log file is * is specified by [[logFile]]. If the size of the log file exceeds
* greater than {@link setMaxFileSize maxFileSize} (in kilo-bytes), a rotation * [[maxFileSize]] (in kilo-bytes), a rotation will be performed,
* is performed, which renames the current log file by suffixing the file name * which renames the current log file by suffixing the file name
* with '.1'. All existing log files are moved backwards one place, i.e., '.2' * with '.1'. All existing log files are moved backwards one place,
* to '.3', '.1' to '.2'. The property {@link setMaxLogFiles maxLogFiles} * i.e., '.2' to '.3', '.1' to '.2', and so on. The property
* specifies how many files to be kept. * [[maxLogFiles]] specifies how many files to keep.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id: CFileLogRoute.php 3001 2011-02-24 16:42:44Z alexander.makarow $ * @since 2.0
* @package system.logging
* @since 1.0
*/ */
class CFileLogRoute extends CLogRoute class FileTarget extends Target
{ {
/** /**
* @var integer maximum log file size * @var integer maximum log file size, in kilo-bytes. Defaults to 1024, meaning 1MB.
*/ */
private $_maxFileSize = 1024; // in KB public $maxFileSize = 1024; // in KB
/** /**
* @var integer number of log files used for rotation * @var integer number of log files used for rotation. Defaults to 5.
*/ */
private $_maxLogFiles = 5; public $maxLogFiles = 5;
/** /**
* @var string directory storing log files * @var string directory storing log files. Defaults to the application runtime path.
*/ */
private $_logPath; public $logPath;
/** /**
* @var string log file name * @var string log file name. Defaults to 'application.log'.
*/ */
private $_logFile = 'application.log'; public $logFile = 'application.log';
/** /**
@ -51,95 +49,37 @@ class CFileLogRoute extends CLogRoute
public function init() public function init()
{ {
parent::init(); parent::init();
if ($this->getLogPath() === null) if ($this->logPath === null) {
$this->setLogPath(Yii::app()->getRuntimePath()); $this->logPath = \Yii::app()->getRuntimePath();
} }
if (!is_dir($this->logPath) || !is_writable($this->logPath)) {
/** throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable.");
* @return string directory storing log files. Defaults to application runtime path. }
*/ if ($this->maxLogFiles < 1) {
public function getLogPath() $this->maxLogFiles = 1;
{ }
return $this->_logPath; if ($this->maxFileSize < 1) {
} $this->maxFileSize = 1;
}
/**
* @param string $value directory for storing log files.
* @throws CException if the path is invalid
*/
public function setLogPath($value)
{
$this->_logPath = realpath($value);
if ($this->_logPath === false || !is_dir($this->_logPath) || !is_writable($this->_logPath))
throw new CException(Yii::t('yii', 'CFileLogRoute.logPath "{path}" does not point to a valid directory. Make sure the directory exists and is writable by the Web server process.',
array('{path}' => $value)));
}
/**
* @return string log file name. Defaults to 'application.log'.
*/
public function getLogFile()
{
return $this->_logFile;
}
/**
* @param string $value log file name
*/
public function setLogFile($value)
{
$this->_logFile = $value;
}
/**
* @return integer maximum log file size in kilo-bytes (KB). Defaults to 1024 (1MB).
*/
public function getMaxFileSize()
{
return $this->_maxFileSize;
}
/**
* @param integer $value maximum log file size in kilo-bytes (KB).
*/
public function setMaxFileSize($value)
{
if (($this->_maxFileSize = (int)$value) < 1)
$this->_maxFileSize = 1;
}
/**
* @return integer number of files used for rotation. Defaults to 5.
*/
public function getMaxLogFiles()
{
return $this->_maxLogFiles;
}
/**
* @param integer $value number of files used for rotation.
*/
public function setMaxLogFiles($value)
{
if (($this->_maxLogFiles = (int)$value) < 1)
$this->_maxLogFiles = 1;
} }
/** /**
* Saves log messages in files. * Sends log [[messages]] to specified email addresses.
* @param array $logs list of log messages * @param boolean $final whether this method is called at the end of the current application
*/ */
protected function processLogs($logs) public function exportMessages($final)
{ {
$logFile = $this->getLogPath() . DIRECTORY_SEPARATOR . $this->getLogFile(); $logFile = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
if (@filesize($logFile) > $this->getMaxFileSize() * 1024) if (@filesize($logFile) > $this->maxFileSize * 1024) {
$this->rotateFiles(); $this->rotateFiles();
$fp = @fopen($logFile, 'a'); }
@flock($fp, LOCK_EX); $messages = array();
foreach ($logs as $log) foreach ($this->messages as $message) {
@fwrite($fp, $this->formatLogMessage($log[0], $log[1], $log[2], $log[3])); $messages[] = $this->formatMessage($message);
@flock($fp, LOCK_UN); }
@fclose($fp); @file_put_contents($logFile, implode('', $messages), FILE_APPEND | LOCK_EX);
$this->messages = array();
} }
/** /**
@ -147,21 +87,21 @@ class CFileLogRoute extends CLogRoute
*/ */
protected function rotateFiles() protected function rotateFiles()
{ {
$file = $this->getLogPath() . DIRECTORY_SEPARATOR . $this->getLogFile(); $file = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
$max = $this->getMaxLogFiles(); for ($i = $this->maxLogFiles; $i > 0; --$i) {
for ($i = $max;$i > 0;--$i)
{
$rotateFile = $file . '.' . $i; $rotateFile = $file . '.' . $i;
if (is_file($rotateFile)) if (is_file($rotateFile)) {
{
// suppress errors because it's possible multiple processes enter into this section // suppress errors because it's possible multiple processes enter into this section
if ($i === $max) if ($i === $this->maxLogFiles) {
@unlink($rotateFile); @unlink($rotateFile);
else }
else {
@rename($rotateFile, $file . '.' . ($i + 1)); @rename($rotateFile, $file . '.' . ($i + 1));
}
} }
} }
if (is_file($file)) if (is_file($file)) {
@rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section @rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section
}
} }
} }

Loading…
Cancel
Save