Tip: how to specify a different log file for tests

If you want to write file logging output to a separate log file during tests, thus avoiding the potential problem of a different user account lacking write permission to the log file, you can add string key(s) to the ‘routes’ array.

Remember to add corresponding key(s) in the main configuration, thus allowing mergeArray() to replace the existing route declaration(s).




return CMap::mergeArray(

  require(dirname(__FILE__).'/main.php'),

  array(

    'components'=>array(

      ...

      'log'=>array(

        'routes'=>array(

          'stdlog' => array(

            'class'=>'CFileLogRoute',

            'logFile'=>'test.log',

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

          'otherlog' => array(

            'class'=>'CFileLogRoute',

            'logFile'=>'other.log',

            'categories'=>'my_category.*',

          ),

          ...



/Tommy

This looks interesting somehow - but i don’t understand how you use the array keys to prevent permission problems. Could you please explain a little more?

With the default 644 file permission, only the file owner will have write access. The web server process doesn’t normally use the same account as a command line user. Of course, it’s possible to remove any existing log file before and after a (command line) test session.

I decided to work around the problem by switching to a different log file name in the test configuration file log route. The easiest way to suspend the main configuration log route would be to have the test configuration replace it. For this to happen, the key has to be of string type, not implicit numeric type. The latter will be renumbered by mergeArray() instead of replacing any existing key.

/Tommy

I see, nice trick.

How do i write the log in to the particular file?

anybody help?

i’d like to generate custom log on the particular controller’s action called. so how do i write the custom log file for this?

You can specify a category in the call to Yii::log().

/Tommy

How do i get all the log data in the particular file?

i’ve get only the current log only.

Can anyone help?

print_r($logger->getLogs(‘info’, ‘mytest’));

it shows only current log not past.

Your question is not related to the tip this thread is about. Please start a new thread preferrably in "General discussion for Yii 1.1".

/Tommy