Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better CFileLogRoute performance

  • Loading branch information...
commit df7e370e23d0d4482b671e4495c3e2febeb036e8 1 parent cf1c20c
@samdark samdark authored
Showing with 4 additions and 1 deletion.
  1. +1 −0  CHANGELOG
  2. +3 −1 framework/logging/CFileLogRoute.php
View
1  CHANGELOG
@@ -14,6 +14,7 @@ Version 1.1.14 work in progress
- Bug #1984: CDbMigration: fix of undeclared variable usage in debug information in dropPrimaryKey (papulovskiy)
- Bug #1996: Using yiic help for commands with parameters with array as default value resulted in PHP error with latest PHP versions (dInGd0nG, samdark)
- Bug #2030: Fixed problem with MySQL 4.x: Undefined Index: Comment in CMysqlSchema (cebe)
+- Enh: Better CFileLogRoute performance (Qiang, samdark)
- Enh #1847: Added COutputCache::varyByLanguage to generate separate cache for different languages (Obramko)
- Enh #2038: CFormatter::formatNtext() method can replace newlines with `<p></p>` not just with `<br />` as it was before (resurtm)
- Chg #645: CDbConnection now throws CDbException when failed to open DB connection instead of failing with a warning (kidol, eirikhm, samdark, cebe)
View
4 framework/logging/CFileLogRoute.php
@@ -140,8 +140,10 @@ protected function processLogs($logs)
@flock($fp,LOCK_EX);
if(@filesize($logFile)>$this->getMaxFileSize()*1024)
$this->rotateFiles();
+ $buffer='';
foreach($logs as $log)
- @fwrite($fp,$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]));
+ $buffer.=$this->formatLogMessage($log[0],$log[1],$log[2],$log[3]);
+ @fwrite($fp,$buffer);
@flock($fp,LOCK_UN);
@fclose($fp);
}

8 comments on commit df7e370

@qiangxue
Owner

It's better to move the buffering out of the flock calls so that the lock takes less time.

Besides the issue about exceeding size limit, the newly written logs will appear in .1 file, and the original log file will "disappear", both of which are unexpected, even though they are trivial.

@samdark
Owner

Any suggestions about not changing behavior?

@qiangxue
Owner

The following is the code in yii 2:

        $text = '';
        foreach ($messages as $message) {
            $text .= $this->formatMessage($message);
        }
        $fp = @fopen($this->logFile, 'a');
        @flock($fp, LOCK_EX);
        if (@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);
        }
@samdark
Owner

Hmm. Why file_put_contents is used in one case and fwrite in another? Is to reuse file resource?

@qiangxue
Owner

Because in the first case, $fp points to the .1 file after file rotation (and that's why the issues I described above).

@monque

@qiangxue
Now that you know the size of buffer write to log,

 if (@filesize($this->logFile) + strlen($text) > $this->maxFileSize * 1024)

Why not use this if test to avoid exceeding size limit ?

@qiangxue
Owner

Yes, this will solve the size problem, but the latest messages will still be written to the .1 file.

Please sign in to comment.
Something went wrong with that request. Please try again.