Skip to content

Commit

Permalink
Fixes yiisoft#8983: Only truncate the original log file for rotation
Browse files Browse the repository at this point in the history
developeruz authored and samdark committed Nov 30, 2017
1 parent 2992b9b commit 9a16f8f
Showing 2 changed files with 42 additions and 13 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #15249: Controllers in subdirectories were not visible in commands list (IceJOKER)
- Enh #5515: Added default value for `yii\behaviors\BlameableBehavior` for cases when the user is guest (dmirogin)
- Bug #14276: Fixed I18N format with dotted parameters (developeruz)
- Bug #8983: Only truncate the original log file for rotation (matthewyang, developeruz)
- Bug #14604: Fixed `yii\validators\CompareValidator` `compareAttribute` does not work if `compareAttribute` form ID has been changed (mikk150)
- Bug #15194: Fixed `yii\db\QueryBuilder::insert()` to preserve passed params when building a `INSERT INTO ... SELECT` query for MSSQL, PostgreSQL and SQLite (sergeymakinen)
- Bug #15229: Fixed `yii\console\widgets\Table` default value for `getScreenWidth()`, when `Console::getScreenSize()` can't determine screen size (webleaf)
54 changes: 41 additions & 13 deletions framework/log/FileTarget.php
Original file line number Diff line number Diff line change
@@ -141,21 +141,49 @@ protected function rotateFiles()
// suppress errors because it's possible multiple processes enter into this section
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));
}
continue;
}
$newFile = $this->logFile . '.' . ($i + 1);
$this->rotateByCopy ? $this->rotateByCopy($rotateFile, $newFile) : $this->rotateByRename($rotateFile, $newFile);
if ($i === 0) {
$this->clearLogFile($rotateFile);
}
}
}
}

/***
* Clear log file without closing any other process open handles
* @param string $rotateFile
*/
private function clearLogFile($rotateFile)
{
if ($filePointer = @fopen($rotateFile, 'a')) {
@ftruncate($filePointer, 0);
@fclose($filePointer);
}
}

/***
* Copy rotated file into new file
* @param string $rotateFile
* @param string $newFile
*/
private function rotateByCopy($rotateFile, $newFile)
{
@copy($rotateFile, $newFile);
if ($this->fileMode !== null) {
@chmod($newFile, $this->fileMode);
}
}

/**
* Renames rotated file into new file
* @param string $rotateFile
* @param string $newFile
*/
private function rotateByRename($rotateFile, $newFile)
{
@rename($rotateFile, $newFile);
}
}

0 comments on commit 9a16f8f

Please sign in to comment.