Yii Loging User ID or IP

Hi everyone!

Is there a way to add user ID or at least IP in the logs when an error occurs?

I’m using yii-user moduele and the log is sent via e-mail using CEmailLogRoute.

There is a section in documentation regarding your problem: http://www.yiiframework.com/doc/guide/1.1/en/topics.logging#logging-context-information

So the easiest solution would be to create custom class extending CLogFilter with property

$logVars containing ‘REMOTE_ADDR’ to indicate that you want to log IP address. Simple implementation:


class MyCustomFilter extends CLogFIlter implements ILogFilter

{

public $logVars=array('_GET','_POST','_FILES','_COOKIE','_SESSION','_SERVER', 'REMOTE_ADDR');

}

and then use this class name in your config file when declaring logging.

Also this filter should support logging your user ID as if you look into CLogFilter code there is userId handled:


protected function format(&$logs)

	{

		$prefix='';

		if($this->prefixSession && ($id=session_id())!=='')

			$prefix.="[$id]";

		if($this->prefixUser && ($user=Yii::app()->getComponent('user',false))!==null)

			$prefix.='['.$user->getName().']['.$user->getId().']';

		if($prefix!=='')

		{

			foreach($logs as &$log)

				$log[0]=$prefix.' '.$log[0];

		}

	}