123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?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);
- }
- if ($this->maxLogFiles < 1) {
- $this->maxLogFiles = 1;
- }
- if ($this->maxFileSize < 1) {
- $this->maxFileSize = 1;
- }
- }
-
- public function export()
- {
- $logPath = dirname($this->logFile);
- FileHelper::createDirectory($logPath, $this->dirMode, true);
- $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) {
- @flock($fp, LOCK_UN);
- @fclose($fp);
- $this->rotateFiles();
- $writeResult = @file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
- if ($writeResult === false) {
- $error = error_get_last();
- throw new LogRuntimeException("Unable to export log through file!: {$error['message']}");
- }
- $textSize = strlen($text);
- if ($writeResult < $textSize) {
- throw new LogRuntimeException("Unable to export whole log through file! Wrote $writeResult out of $textSize bytes.");
- }
- } else {
- $writeResult = @fwrite($fp, $text);
- if ($writeResult === false) {
- $error = error_get_last();
- throw new LogRuntimeException("Unable to export log through file!: {$error['message']}");
- }
- $textSize = strlen($text);
- if ($writeResult < $textSize) {
- throw new LogRuntimeException("Unable to export whole log through file! Wrote $writeResult out of $textSize bytes.");
- }
- @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);
- continue;
- }
- $newFile = $this->logFile . '.' . ($i + 1);
- $this->rotateByCopy ? $this->rotateByCopy($rotateFile, $newFile) : $this->rotateByRename($rotateFile, $newFile);
- if ($i === 0) {
- $this->clearLogFile($rotateFile);
- }
- }
- }
- }
-
- private function clearLogFile($rotateFile)
- {
- if ($filePointer = @fopen($rotateFile, 'a')) {
- @ftruncate($filePointer, 0);
- @fclose($filePointer);
- }
- }
-
- private function rotateByCopy($rotateFile, $newFile)
- {
- @copy($rotateFile, $newFile);
- if ($this->fileMode !== null) {
- @chmod($newFile, $this->fileMode);
- }
- }
-
- private function rotateByRename($rotateFile, $newFile)
- {
- @rename($rotateFile, $newFile);
- }
- }
|