Logging to a file

I’m trying to get started with Yii. I have created a demo application using the yiic. This is working fine.

I’d like a log of all that executes when I view the index.php page.

Under config in the main.php file I have this coded:




// application components

'components'=>array(

   'user'=>array(

      // enable cookie-based authentication

      'allowAutoLogin'=>true,

),

'log'=>array(

   'class'=>'CLogRouter',

   'routes'=>array(

      array(

         'class'=>'CFileLogRoute',

         'levels'=>'trace, info, error, warning',

         'logFile'=>'msgs.log',

      ),

   ),

),



and in the SiteController.php I have added this:




    /**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

    public function actionIndex()

    {

Yii::log('right here');

...




I’m expecting a file called msgs.log to be created in the runtime directory underneath my application.

There is nothing in there. Yii-1.1.4.r2429

Any idea would be appreciated.

  • maestroQC

Do you preload the log component?




'preload'=>array('log'),

'components'=>array(...),

I believe so I have this line of code in the main.php file:




// preloading 'log' component

'preload'=>array('log'),


// autoloading model and component classes

'import'=>array(

   'application.models.*',

   'application.components.*',

),


'modules'=>array(

   // uncomment the following to enable the Gii too

   /*

   'gii'=>array(

      'class'=>'system.gii.GiiModule',

      'password'=>'Enter Your Password Here',

    ),

    */

),


// application components

'components'=>array(

   'user'=>array(

      // enable cookie-based authentication

      'allowAutoLogin'=>true,

   ),

   'log'=>array(

      'class'=>'CLogRouter',

         'routes'=>array(

            array(

                'class'=>'CFileLogRoute',

                'levels'=>'trace, info, error, warning',

                'logFile'=>'msgs.log',

            ),

         ),

   ),



Can you configure a web log route for a test? Just to verify you don’t have any permission issues. Other than that your config looks o.k.





            'routes'=>array(

                array(                                  // Show logs at end of every page

                    'class'=>'CWebLogRoute',

                    'categories'=>'system.*,application.*',

                ),



Alright, I have added the additional route and I cannot see anything in the index.php page (thru my browser) that would leave me to believe that something was logged.

Ok, so i would try to get at least any logging before you can figure out what’s wrong. More things to check:

  • Set YII_DEBUG true. This should give you a mass of trace output on each request.

  • Do not use yiilite.php as the Yii::trace() calls where stripped of here for performance reasons

I have this in my index.php file. Does this suffice?

Or should I create a yii.php file?




<?php


// change the following paths if necessary

$yii=dirname(__FILE__).'/../yii/framework/yii.php';

$config=dirname(__FILE__).'/protected/config/main.php';


// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each log message

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);


require_once($yii);

Yii::createWebApplication($config)->run();



Yes, that looks fine. This is strange, you should at least get something. Do you do something unconventional in your action? Like call "exit"?

This is what is in the protected/controllers/SiteController.php




<?php


class SiteController extends Controller

{

    /**

     * Declares class-based actions.

     */

    public function actions()

    {

        return array(

            // captcha action renders the CAPTCHA image displayed on the contact page

            'captcha'=>array(

                'class'=>'CCaptchaAction',

                'backColor'=>0xFFFFFF,

            ),

            // page action renders "static" pages stored under 'protected/views/site/pages'

            // They can be accessed via: index.php?r=site/page&view=FileName

            'page'=>array(

                'class'=>'CViewAction',

            ),

        );

    }


    /**

     * This is the default 'index' action that is invoked

     * when an action is not explicitly requested by users.

     */

    public function actionIndex()

    {

        Yii::log('right here');


        // renders the view file 'protected/views/site/index.php'

        // using the default layout 'protected/views/layouts/main.php'

        $this->render('index');

    }


    /**

     * This is the action to handle external exceptions.

     */

    public function actionError()

    {

        if($error=Yii::app()->errorHandler->error)

        {

            if(Yii::app()->request->isAjaxRequest)

                echo $error['message'];

            else

                $this->render('error', $error);

        }

    }


    /**

     * Displays the contact page

     */

    public function actionContact()

    {

        $model=new ContactForm;

        if(isset($_POST['ContactForm']))

        {

            $model->attributes=$_POST['ContactForm'];

            if($model->validate())

            {

                $headers="From: {$model->email}\r\nReply-To: {$model->email}";

                mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);

                Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');

                $this->refresh();

            }

        }

        $this->render('contact',array('model'=>$model));

    }


    /**

     * Displays the login page

     */

    public function actionLogin()

    {

        $model=new LoginForm;


        // if it is ajax validation request

        if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')

        {

            echo CActiveForm::validate($model);

            Yii::app()->end();

        }


        // collect user input data

        if(isset($_POST['LoginForm']))

        {

            $model->attributes=$_POST['LoginForm'];

            // validate user input and redirect to the previous page if valid

            if($model->validate() && $model->login())

                $this->redirect(Yii::app()->user->returnUrl);

        }

        // display the login form

        $this->render('login',array('model'=>$model));

    }


    /**

     * Logs out the current user and redirect to homepage.

     */

    public function actionLogout()

    {

        Yii::app()->user->logout();

        $this->redirect(Yii::app()->homeUrl);

    }

}

I’m sorry, but i’m out of ideas. We must miss something very obvious here (like wrong config file or so). Logging usually works pretty fine.

Thanks for your time and patience, much appreciated!

Yes I’m not off to a good start, I hope this doesn’t dampen my outlook on the Yii framework.

Found the problem. The directory of the code did not have all of the correct permissions. :o

You did mention this possible path.

I have also turned on the debugging in PHP.

Thanks again for the support.

Glad you solved it. Even though it doesn’t really explain why CWebLogRoute didn’t work. Which directory had wrong permissions?

I my haste to find the problem, I had created a new project. This new project was not in the correct path to be executed by apache/php. Obviously it did not have the correct permissions either.

So every time we attempted to debug it, the old version of the application was being executed.

Yeah its one of those really ugly oversights in problem solving… it hurts!