How To Add Error Log

I wrote one function to send mail in component. I have to add all the email when Email failed. How to add new file and add this error




        if(!$mail->Send()) {

            //echo "Mailer Error: " . $mail->ErrorInfo;

           I need error log here

        }else {

            echo "Message sent!";

        }



Dear Brother

Let us assume that our email sending logic resides in SiteController.php.

Then we can add a line for error logging in the following way.




if(!$mail->Send()) 

{

            //echo "Mailer Error: " . $mail->ErrorInfo;


           Yii::log("Mailer Error: " . $mail->ErrorInfo,'error','application.controllers.SiteController');


           //Second parameter is the level of message('error','info',...);

           //Third parameter is category which is actually meaning place of logging.

}

else 

{

            echo "Message sent!";

}



In main configuration file we have to add a new route in log component for logging this errors.

Main.php




'log'=>array(

			'class'=>'CLogRouter',

			'routes'=>array(

				array(

					'class'=>'CFileLogRoute',

					'levels'=>'error, warning',

				),


                              //The following router application component is added.

                                array(

					'class'=>'CFileLogRoute',

					'levels'=>'error',

					'categories'=>array('application.controllers.SiteController'),

					'logFile'=>'mailError.log',

				),

				// uncomment the following to show log messages on web pages

				/*

				array(

					'class'=>'CWebLogRoute',

				),

				*/

			),

		),




Now any errors is saved in the protected/runtime/mailError.log which is created by YII itself.

I hope I helped a bit.

Cheers.

Thanks brother.

I wrote code for log file using your response




                array(

                    'class'=>'CFileLogRoute',

                    'levels'=>'mailerror',

                    'categories'=>"system.*",

                    'logFile'=>'mailError.log',

                ),