Yii Framework Forum: Logging and CWebLogRoute - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Logging and CWebLogRoute ...or how to make your own messages stand out Rate Topic: ***** 2 Votes

#1 User is offline   Techie42 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-November 10
  • Location:Berkshire, UK

Posted 16 November 2010 - 11:47 AM

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:
2

#2 User is offline   derelict 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 64
  • Joined: 18-November 09
  • Location:Ukraine

Posted 16 November 2010 - 12:00 PM

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

#3 User is offline   Techie42 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-November 10
  • Location:Berkshire, UK

Posted 16 November 2010 - 12:12 PM

View Postderelict, on 16 November 2010 - 12:00 PM, said:

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!
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 16 November 2010 - 02:09 PM

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 ".").

This post has been edited by Mike: 16 November 2010 - 02:11 PM

0

#5 User is offline   Techie42 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-November 10
  • Location:Berkshire, UK

Posted 17 November 2010 - 10:36 AM

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
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 November 2010 - 10:39 AM

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

#7 User is offline   Techie42 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-November 10
  • Location:Berkshire, UK

Posted 17 November 2010 - 11:30 AM

View PostMike, on 17 November 2010 - 10:39 AM, said:

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)
0

#8 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 17 November 2010 - 11:42 AM

View PostTechie42, on 17 November 2010 - 11:30 AM, said:

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.yiiframew...Base#log-detail

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#9 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 17 November 2010 - 12:18 PM

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.
1

#10 User is offline   Techie42 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 17
  • Joined: 16-November 10
  • Location:Berkshire, UK

Posted 17 November 2010 - 12:26 PM

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 :)
0

#11 User is offline   jb.bhavik 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 03-April 14

Posted 23 July 2014 - 12:27 AM

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.
Thank You
Jb.

Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users