Yii Message Logging

Here I have explained about "log" concept using file. It is available in yii. Just i used one new level log and saved this log information in individual file. Just add the following code in config/main.php

Add Log in main.php

I created one new level for my "mailError.log" file. When error occured the "mailError.log" file will create automatically and log information will save into that file.

<?php
'log'=>array(
    'class'=>'CLogRouter',
    'routes'=>array(
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'mailerror',
            'categories'=>"system.*",
            'logFile'=>'mailError.log',
        ),                				                
       
        array(
            'class'=>'CFileLogRoute',
            'levels'=>'error, warning',
        ),
    ),
),
?>

Save log into File

When model save failed or some failure case, I will add one message into "log" file using below code. We can use this anywhere in our application. Just we have to mention the levels of log. Here i added "mailerror" level.

<?php
if($mail->save()) 
{
    echo "Success";
}else
{
    Yii::log("Mailer Error ",'mailerror','system.*');
}
?>


  • anil

    i tried this but this is not working. i copy paste the same code but still not able to genrate new log file with new message

    • mbala

      Hi anil, It is working fine only. I tried following code to create the new log file with new message
      In localhost, mail function showing the mailserver error. So i called the error log directly. See below code and try this in actionContact

      Yii::log(“Mailer Error “,’mailerror’,’system.*’);//direct call
      if(mail(Yii::app()->params[‘adminEmail’],$subject,$model->body,$headers)) {
      echo “Success”;
      }else {
      Yii::log(“Mailer Error “,’mailerror’,’system.*’);
      }