You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.9 KiB

14 years ago
<?php
/**
14 years ago
* FileTarget class file.
14 years ago
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
14 years ago
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\logging;
14 years ago
/**
14 years ago
* FileTarget records log messages in files.
14 years ago
*
14 years ago
* The log files are stored under [[logPath]] and their name
* is specified by [[logFile]]. If the size of the log file exceeds
* [[maxFileSize]] (in kilo-bytes), a rotation will be performed,
* 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' to '.3', '.1' to '.2', and so on. The property
* [[maxLogFiles]] specifies how many files to keep.
14 years ago
*
* @author Qiang Xue <qiang.xue@gmail.com>
14 years ago
* @since 2.0
14 years ago
*/
14 years ago
class FileTarget extends Target
14 years ago
{
/**
14 years ago
* @var integer maximum log file size, in kilo-bytes. Defaults to 1024, meaning 1MB.
14 years ago
*/
14 years ago
public $maxFileSize = 1024; // in KB
14 years ago
/**
14 years ago
* @var integer number of log files used for rotation. Defaults to 5.
14 years ago
*/
14 years ago
public $maxLogFiles = 5;
14 years ago
/**
14 years ago
* @var string directory storing log files. Defaults to the application runtime path.
14 years ago
*/
14 years ago
public $logPath;
14 years ago
/**
14 years ago
* @var string log file name. Defaults to 'application.log'.
14 years ago
*/
14 years ago
public $logFile = 'application.log';
14 years ago
/**
* Initializes the route.
* This method is invoked after the route is created by the route manager.
*/
public function init()
{
parent::init();
14 years ago
if ($this->logPath === null) {
13 years ago
$this->logPath = \Yii::$application->getRuntimePath();
14 years ago
}
if (!is_dir($this->logPath) || !is_writable($this->logPath)) {
throw new \yii\base\Exception("Directory '{$this->logPath}' does not exist or is not writable.");
}
if ($this->maxLogFiles < 1) {
$this->maxLogFiles = 1;
}
if ($this->maxFileSize < 1) {
$this->maxFileSize = 1;
}
14 years ago
}
/**
14 years ago
* Sends log [[messages]] to specified email addresses.
* @param boolean $final whether this method is called at the end of the current application
14 years ago
*/
14 years ago
public function exportMessages($final)
14 years ago
{
14 years ago
$logFile = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
if (@filesize($logFile) > $this->maxFileSize * 1024) {
14 years ago
$this->rotateFiles();
14 years ago
}
$messages = array();
foreach ($this->messages as $message) {
$messages[] = $this->formatMessage($message);
}
@file_put_contents($logFile, implode('', $messages), FILE_APPEND | LOCK_EX);
14 years ago
}
/**
* Rotates log files.
*/
protected function rotateFiles()
{
14 years ago
$file = $this->logPath . DIRECTORY_SEPARATOR . $this->logFile;
for ($i = $this->maxLogFiles; $i > 0; --$i) {
14 years ago
$rotateFile = $file . '.' . $i;
14 years ago
if (is_file($rotateFile)) {
14 years ago
// suppress errors because it's possible multiple processes enter into this section
14 years ago
if ($i === $this->maxLogFiles) {
14 years ago
@unlink($rotateFile);
13 years ago
} else {
14 years ago
@rename($rotateFile, $file . '.' . ($i + 1));
14 years ago
}
14 years ago
}
}
14 years ago
if (is_file($file)) {
14 years ago
@rename($file, $file . '.1'); // suppress errors because it's possible multiple processes enter into this section
14 years ago
}
14 years ago
}
}