Yii::setLogger function to provide custom logger

It would be nice if you could provide custom logger, that logs more context parameters that can be used by custom logroutes.

For example - logger could log current value of Yii::app()->user->login, and custom logroute could use that information (or not).

Now you can fetch that information in LogRoute when processing logs, but the information could change during program execution and logs are stored at the end of request. It can lead to unsuspected logs, like in this situation:

Yii::log( 'User log out' );


when logroute get logs to store at the end of request processing, the "user" context says that currently "Guest" is logged, which was not true when log occured.

I know that you could call "Yii::getLogger()->flush()" after call to "log" function but this is not pretty and developer has to remember that it is required.

Another solution - setting "autoFlush=0" value forces every log to be stored immediately, which is also not very good.

The best way is to provide custom Logger and LogRoute, but now you cannot override private $_logger attribute of YiiBase object…

I already asked this for a while. It was a long time ago and my post disappeared…

I had to change the private to protected on CLogger:_logs, CLogger_logCount, to write Yii::log, and so on. It was so annoying during the upgrades and deployments (I had to apply some patches) that I’m copying code from CLogger to MyLogger.

As of 1.1.8 there is autoDump property in CLogger witch will dump log records straight away to file. Also they fixed the issue of autoFlush not flushing logs to file when the message amount was reached.

So now it all works just fine and 1.1.8 should be out very soon.

Had a similar problem and resolved it by just writing my own Yii class, derived from YiiBase (the default implementation is empty anyway). Have my own custom log() function and custom logger.

There is one caveat though: You can’t use yiilite.php, but that is not such a big problem.

Qiang replied to a question of mine (on how to override Yii with yiilite) that it will become obsolete pretty soon anyway.

Like I wrote before - it is not the same. When there is a lot of log entries - dumping them once at the and can have significant performance boost comparing to writing them as soon as they arise…

Fortunately in current release (1.1.8) there is already setter setLogger, which was added to Yii class after my issue report (http://code.google.com/p/yii/issues/detail?id=2535), so you can migrate your code and start using official interface and yiilite :)