Logger and flush problem

I tried to view application’s log before script complete.

I’ve read that CLogger class in Yii 1.1 has autoFlush property and flush() method to manage log flushing.

My script will be run for long time (contains large loop with network connection), so I need to see logged message before my script ends.

my approach (test case):




		set_time_limit(0);

			

		for($i = 1; $i <= 5; $i++) {

			echo 'appending log at ' . date('Y-m-d H:i:s') . '<br />';

			Yii::log('warning at ' . date('Y-m-d H:i:s'), 'warning');

			$logger->flush();

			sleep(5);

		}



I monitored log file and see all logs still appears at once after script completed. What did I do wrong?

I also tried with autoFlush property:




		$logger = Yii::getLogger();

		$logger->autoFlush = 2;



Both method still output logs after script completed.

The problem is that the autoFlush just forces the respective logRoutes to collect the applicable entries from the logger so that the logger can empty its queue. The actual dumping of the logEntries happens on the "onEndRequest" event, not on "onFlush".

What you can do to force this is call “Yii::app()->log->processLogs(new CEvent($this));” (it needs an Event as argument to fake the call). I haven’t tested it but I’m fairly confident it should work and solve your problem.


Yii::getLogger()->flush(true)

Working perfect for me…