Yii – more Advanced Logging

Yii uses an application component called CLogRouter to determine where and how log messages are output to the various logs. You can configure different “log routes” depending on the log trace level, the log category (more on that later) and even your IP address.

I’ve already covered some configurations in my article “Advanced Logging in Yii” that covers enabling profiling in Firefox, but recently I needed to increase the number of log files that were being kept by the server.  I was trying to hunt down a particularly elusive bug when the events happened over a weekend.  By the time I got to look into it on the Monday morning, all the logs had rolled forward and lost the necessary information.

So, I started looking into how to extend the log rolls and even hold info information in another log file.  If you’ve read my previous article you will have seen that I have included a logging function in my bootstrap index.php file called ‘fb’ which outputs to the log file.

In my code, I can just write fb(“Some message $variable”,”category.hierarchy”) or even just fb($variable).

So, I changed my fb function to prepend all categories with “Application.” as follows:-

function fb($what,$where='fb.somewhere'){
    $what=print_r($what,true);
    Yii::log($what, 'info', 'application.'.$where);
}

I then decided to output all info messages to a separate log file called application_info and extend the number of versions of this file from the default 5 to 10.  I also included the category ‘system.db.CDbCommand’ which logs SQL from CdbCommand objects where I often use more complex joins. All other messages would still get logged to the default application log.

  'log'=>array(
 'class'=>'CLogRouter',
 'routes'=>array(
                             array(
                                'class'=>'CFileLogRoute',
                                'levels'=>'info, vardump',
                                'logFile'=>'application_info',
                                'maxLogFiles'=>10
                            ),
                             array(
                                'class'=>'CFileLogRoute',
                                'levels'=>'trace',
                                'logFile'=>'application_info',
                                'maxLogFiles'=>10,
                                'categories'=>'system.db.CDbCommand'
                            ),
                            array(
                                'class'=>'CFileLogRoute',
                                'levels'=>'trace, error, warning',
                            ),
                           array(
                                'class'=>'ext.yii-debug-toolbar.YiiDebugToolbarRoute',
                                'ipFilters'=>array('127.0.0.1','192.168.1.1'),
                            ),
                         /*   array(
                                'class'=>'CFileLogRoute',
                                'levels'=>'trace, info, error, warning, vardump', //info, error, warning, vardump',
                            ),
                            array(
                                'class'=>'CEmailLogRoute',
                                'levels'=>'error, warning',
                                'emails'=>'[email protected]',
                            ),*/
 ),
 ),

I wanted to use the ‘except’ parameter as outlined on The Definitive Guide to Yii but got an error “Property “CFileLogRoute.except” is not defined.” but couldn’t find anything more about this property …. feedback welcome!

Let’s Start a Project!

Contact Me