Log a log out.

Hi.

I want to log who logged out using Yii::log()

I set ‘prefixUser’=>true in my conf file. So, user names are automatically recorded in the log file.

At first, I did like this.




Yii::log('user logout', 'info', 'system.controllers.site');

Yii::app()->user->logout(true);



But, processLogs is called after removing the session data. So, in this case, the log file is like this.




[info] [system.controllers.site] [Guest][] citizen user logout



There is no name in the log file. But I want to record who logged out. so I called




Yii::app()->log->processLogs(new CEvent($this));



befor logout() called.

It worked well, but I have 2 log lines because processLogs called twice, in my code and onEndRequest event.




[info] [system.controllers.site] [username][1] citizen user logout

[info] [system.controllers.site] [Guest][] citizen user logout



Can I flush log data or is there any solution for my case?

I found a solution.




    Yii::log('user logout', 'info', 'system.controllers.site');

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

    Yii::app()->log->processLogs(new CEvent($this));

    Yii::app()->detachEventHandler('onEndRequest',array(Yii::app()->log,'processLogs'));

    Yii::app()->user->logout(true);



The problem is that processLogs is only called when onEndRequest is raised. If I call processLogs manually, processLogs will be called twice. In addition, even if I call flush(), this function does not flush current session names and ids(Yii::app()->user). So, when CLogFilter::format is called, user session has already cleared.