yii\log\Logger::flush PHP Method

flush() public method

Flushes log messages from memory to targets.
public flush ( boolean $final = false )
$final boolean whether this is a final call during a request.
    public function flush($final = false)
    {
        $messages = $this->messages;
        // https://github.com/yiisoft/yii2/issues/5619
        // new messages could be logged while the existing ones are being handled by targets
        $this->messages = [];
        if ($this->dispatcher instanceof Dispatcher) {
            $this->dispatcher->dispatch($messages, $final);
        }
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider booleanDataProvider
  */
 public function testRotate($rotateByCopy)
 {
     $logFile = Yii::getAlias('@yiiunit/runtime/log/filetargettest.log');
     FileHelper::removeDirectory(dirname($logFile));
     mkdir(dirname($logFile), 0777, true);
     $logger = new Logger();
     $dispatcher = new Dispatcher(['logger' => $logger, 'targets' => ['file' => ['class' => 'yii\\log\\FileTarget', 'logFile' => $logFile, 'levels' => ['warning'], 'maxFileSize' => 1024, 'maxLogFiles' => 1, 'logVars' => [], 'rotateByCopy' => $rotateByCopy]]]);
     // one file
     $logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
     $logger->flush(true);
     clearstatcache();
     $this->assertTrue(file_exists($logFile));
     $this->assertFalse(file_exists($logFile . '.1'));
     $this->assertFalse(file_exists($logFile . '.2'));
     $this->assertFalse(file_exists($logFile . '.3'));
     $this->assertFalse(file_exists($logFile . '.4'));
     // exceed max size
     for ($i = 0; $i < 1024; $i++) {
         $logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
     }
     $logger->flush(true);
     // first rotate
     $logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
     $logger->flush(true);
     clearstatcache();
     $this->assertTrue(file_exists($logFile));
     $this->assertTrue(file_exists($logFile . '.1'));
     $this->assertFalse(file_exists($logFile . '.2'));
     $this->assertFalse(file_exists($logFile . '.3'));
     $this->assertFalse(file_exists($logFile . '.4'));
     // second rotate
     for ($i = 0; $i < 1024; $i++) {
         $logger->log(str_repeat('x', 1024), Logger::LEVEL_WARNING);
     }
     $logger->flush(true);
     clearstatcache();
     $this->assertTrue(file_exists($logFile));
     $this->assertTrue(file_exists($logFile . '.1'));
     $this->assertFalse(file_exists($logFile . '.2'));
     $this->assertFalse(file_exists($logFile . '.3'));
     $this->assertFalse(file_exists($logFile . '.4'));
 }