Yii Logging Tips and Reference

Yii Logging Tips and Reference

Last updated:

Adding stuff to beforeDelete(), beforeSave()

You probably hook calls to Yii::log in one of these functions. Be careful to call return parent::beforeSave() (or return parent::beforeDelete) (not just call the method, call return too!)

//GOOD
public function beforeDelete() {
    Yii::log("Writing to log",  CLogger::LEVEL_WARNING,'foo.bar');
    return parent::beforeDelete();
}
//BAD
public function beforeDelete() {
    Yii::log("Writing to log",  CLogger::LEVEL_WARNING,'foo.bar');
    parent::beforeDelete(); // no return
}

Add categories to your calls to Yii::log

Otherwise they don't work. A sample complete call to Yii::log would be:

Yii::log("this is my log message", CLogger::LEVEL_WARNING, "mycategory.mysubcategory");

Sample config file

This will save all log messages with level ranging from CLogger::LEVEL_ERROR to CLogger::LEVEL_INFO having categories matching "mycategory.*" (note that it would match the call in the previous example because its category is "mycategory.mysubcategory") and it will write it to protected/runtime/mycustomfile1

'log' => [
    'class' => 'CLogRouter',
    'routes' => [
        [
            'class' => 'CFileLogRoute',
            'levels' => 'error,warning,info',
            'categories'=>'mycategory.*',
            'logFile'=>'mycustomfile1'
        ],
        //... other configs...