Enable Logging only on specific Modules and Users

Hello!

I am now looking for a solution for this for a couple of days now.

Some of my modules are already live, while some modules are still in production. Therefore I would like to be able to enable logging in Webbrowser (CWebLogRoute and CProfileLogRoute) just in a few modules. It would also help, when I could activate logging just for a few users.

As I am asking about logging, I have another question, Is there a better way to see the content of a variable than using print_f and outputting it within the layout. Maybe there is a variable debugger in Yii?!?!

Thank you!

Try to use CVarDumper when debugging your variable.

Why don’t you try encapsulating your log code in a class to deal with these responsibilities? What do you think?

First define a string key to the log route and set enabled to false e.g.




'log'=>array(

  ...

  'class'=>'CLogRouter',

  'routes'=>array(

    'weblogging'=>array(

      'class'=>'CWebLogRoute',

      'enabled'=>false,

    ),

  ),

),



Then in places where you want to enable this particular route




Yii::app()->log->routes['weblogging']->enabled = true;



/Tommy

Thanks guys! I will try this asap.

I quickly tested CVarDumper, looks good, but it is not exactly what I was thinking about. The class outputs the variable right where I put it in the source code. I was looking for something like console.log() in javascript or what I just tried Yii::log(), but I can just enter a string here… Is there a better solution?

Thanks!

Surely you can log a variable with Yii::log(). And if you consider file logging, you can define a route that logs your custom category to a different file.

This will work (there may be better choices than print_r)




  Yii::log('log a var: '.print_r($_GET,true), 'info', 'mycategory');



/Tommy

Thank you! I will be using this from now on.

For specific module, you can call Yii::app()->config() in the module constructor to override the log component setup.

but for specific users, I don’t know a good place to put the code

Doing this results me in the following error:

Notice: Indirect modification of overloaded element of CMap has no effect in C:\xampp\htdocs\…\components\Controller.php on line 34

Indirect modification of overloaded element of CMap has no effect

I tried to enable like this

		//Enable logging only for developers


		if(in_array($uid, Yii::app()->params['developers']))


			Yii::app()->log->routes['weblogging']->enabled = true;	

Any idea?

No, not at this very moment.

I don’t get the warning message, which may explain why I didn’t add the usual “untested” disclaimer to my previous post. Obviously what I suggested doesn’t take effect.

Just checked if setting enabled in config will take effect. Seems like it doesn’t. (I based my suggested solution on this.)

/Tommy

I did some testing.

Enabling a log route in code will not take effect until messages are flushed from memory to the log route.

A log route can be temporary disabled like this




  ...

  Yii::getLogger()->flush();  // collect previously logged messages

  Yii::app()->log->routes['weblogging']->enabled = false;

  ...

  ... (no 'weblogging' here)

  ...

  Yii::getLogger()->flush();  // collect messages for enabled routes only

  Yii::app()->log->routes['weblogging']->enabled = true;

  ...



/Tommy

If you want the flushed messages to actually be output, then you have to pass true to flush()


Yii::getLogger()->flush( true );

I’m not sure why true isn’t the default :blink: