|
- <?php
-
-
- namespace yii\log;
-
- use Yii;
- use yii\base\InvalidConfigException;
- use yii\helpers\FileHelper;
-
-
- class FileTarget extends Target
- {
-
-
- public $logFile;
-
-
- public $enableRotation = true;
-
-
- public $maxFileSize = 10240;
-
-
- public $maxLogFiles = 5;
-
-
- public $fileMode;
-
-
- public $dirMode = 0775;
-
-
- public $rotateByCopy = true;
-
-
-
-
- public function init()
- {
- parent::init();
- if ($this->logFile === null) {
- $this->logFile = Yii::$app->getRuntimePath() . '/logs/app.log';
- } else {
- $this->logFile = Yii::getAlias($this->logFile);
- }
- $logPath = dirname($this->logFile);
- if (!is_dir($logPath)) {
- FileHelper::createDirectory($logPath, $this->dirMode, true);
- }
- if ($this->maxLogFiles < 1) {
- $this->maxLogFiles = 1;
- }
- if ($this->maxFileSize < 1) {
- $this->maxFileSize = 1;
- }
- }
-
-
-
- public function export()
- {
- $text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";
- if (($fp = @fopen($this->logFile, 'a')) === false) {
- throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
- }
- @flock($fp, LOCK_EX);
- if ($this->enableRotation) {
-
-
- clearstatcache();
- }
- if ($this->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {
- $this->rotateFiles();
- @flock($fp, LOCK_UN);
- @fclose($fp);
- @file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
- } else {
- @fwrite($fp, $text);
- @flock($fp, LOCK_UN);
- @fclose($fp);
- }
- if ($this->fileMode !== null) {
- @chmod($this->logFile, $this->fileMode);
- }
- }
-
-
-
- protected function rotateFiles()
- {
- $file = $this->logFile;
- for ($i = $this->maxLogFiles; $i >= 0; --$i) {
-
- $rotateFile = $file . ($i === 0 ? '' : '.' . $i);
- if (is_file($rotateFile)) {
-
- if ($i === $this->maxLogFiles) {
- @unlink($rotateFile);
- } else {
- if ($this->rotateByCopy) {
- @copy($rotateFile, $file . '.' . ($i + 1));
- if ($fp = @fopen($rotateFile, 'a')) {
- @ftruncate($fp, 0);
- @fclose($fp);
- }
- if ($this->fileMode !== null) {
- @chmod($file . '.' . ($i + 1), $this->fileMode);
- }
- } else {
- @rename($rotateFile, $file . '.' . ($i + 1));
- }
- }
- }
- }
- }
- }
|