madi
madi

Reputation: 5662

Yii does not log with an infinite loop

I am not sure if this is the bug in yii or i am doing a wrong approach. I am creating a back-end process that runs without stop and logging is something that is crucial.

I created a console application. My logging works when I do this:

public function actionTest(){
         Yii::log( "testing", 'error', 'worker.*');
         return;
}

I am able to see "testing" logged into the file worker.log that is located in runtime folder>

However when I create an infinite loop, I don't see anything that is logged:

public function actionTest(){
       while(1){
             Yii::log( "testing", 'error', 'worker.*');
             echo 'running';
            usleep(5000000);
       }
}

The worker.log file is empty.

Here is the configuration setting for worker.log in console.php:

array(
   'class'=>'CFileLogRoute',
   'categories' => 'worker.*',
   'logFile' => 'worker.log'
)

Upvotes: 1

Views: 445

Answers (1)

Valentin Rodygin
Valentin Rodygin

Reputation: 864

For performance reasons, by default Yii outputs log records at the end of the request OR when CLogger::autoFlash messages limit is reached IF CLogger::autoDump is set to true.

In your case this event never occurs. To fix it you have several options:

  • configure CLogger to flush messages as soon as you log them, by setting autoFlash = 1 and autoDump = true
  • call CLogger::flush() method manually
  • you can create your own logger class that flushes messages the way you need
  • or use one of the existing implementations that flushes messages right away, like this one.

Upvotes: 2

Related Questions