Logging and CWebLogRoute

During the development stage, I wanted a way to make my own log messages easier to identify in the (potentially) long list at the bottom of the page when using CWebLogRoute.

I know we can apply different message levels to different log routes, but I still wanted to see all the other messages.

So, I decided to add two CWebLogRoute entries to my main configuration:




....

,'components' => array(

			'log' => array(

					'class'	  => 'CLogRouter'

					,'routes' => array(

								array(

									'class'	  => 'CWebLogRoute'

									,'levels' => 'mine'

									)

								,array(

									'class'   => 'CWebLogRoute'

									)

							)

					)

			)


....



This causes two “application log” boxes to appear at the end of the web page, in the sequence listed above, using the defined levels (the second array has no ‘levels’, so displays all log entries).

Then, wherever I use Yii::log(…), I specify ‘mine’ as the level. This causes my log message(s) to appear in both “application log” boxes. The first log box shows only my messages with a level of “mine”, and the second log box shows all messages, with mine (or “mine”, if you will) appearing in the relevant position in the sequence of events.

Sorry if this is obvious to the masses, but I thought I could make my own small contribution in case there are other noobs like me out there :rolleyes:

I think that good idea is extending CWebLogRoute so that your messages (for example, identified by category) were marked in general list with color.

I agree that may be one way of doing it, but I wanted my own messages to "float" to the top of the logs so that I did not need to scroll through all of the messages.

Also, the way I achieved it did not require any modifications to code other than a couple of lines in the configuration.

I suppose it is down to personal preference…thanks for the suggestion :) and for being my first responder!

Hehe, a little hackish (i’ve never thought about bringing up my own levels), because the guide states that they should be one of trace, info, profile, warning or error. But if it works, why not. Interesting approach, though. :).

EDIT:

On second thought: Maybe using a custom category for your logs would be the better idea? You can even filter different sub categories (separated with ".").

In fact, I was surprised that Yii allowed me to do it. I checked through the source code and couldn’t find anywhere that restricted the level types, so I wondered what would happen if I tried.

If levels are ever defined / restricted in future Yii releases, then the approach would fall over, but until then, it solved my problem :)

Maybe we should make a “feature request” that the levels are never restricted, just so that we have the flexibility to take whatever path works for us. After all, no extra work would be generated for the Yii developers to “add” this as a feature :D

Again: Why not categories? Much cleaner and you can make them up as you want.

Sorry Mike, I misunderstood what you meant. I read “categories” but my brain’s regex parser threw out “levels” :wacko:

How would you use implement categories? (Don’t forget, I’m a noob)

Third parameter in Yii::log() call, ‘categories’ filter in log route

http://www.yiiframework.com/doc/api/1.1/YiiBase#log-detail

/Tommy

Log:


Yii::log('My app made a booboo!', 'info','mycomponent');

Yii::log('Something strange happened!','warning','mycomponent.subcategory');



Filter in log route:




 array(

    'class'=>'CWebLogRoute',

    'categories'=>'mycomponent.*',  // catch both messages

),

 array(

    'class'=>'CWebLogRoute',

    'categories'=>'mycomponent.subcategory.*',  // catch only mycomponent.subcategory messages

),



I often use categories like ‘application.controller.site.contact’ to easily filter messages for a specific controller/action.

Thanks Mike and Tri :D

Categories are a much more elegant solution, and an "approved" approach.

Just for curiosity’s sake, I use a category of ‘mine.{class name}.{function name}’, and a level of ‘info’, which causes my messages to appear a different colour in the complete log, and also allows me to group my messages (as per my initial post above). The bonus is that I can clearly see what is generating the messages, without having to give extra information in the message itself.

A very happy bunny :)

Hello everyone,

can you pls list the categories which are to be filtered in CWebLogRoute. I’m new to Yii and serched bt I found only “application”,“system.db.ar”

Any link pls forward.