Yii2 Bootstrap 3
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.

109 lines
3.0 KiB

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