Yii Framework Forum: Log messages except some categories - Yii Framework Forum

Jump to content

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

Log messages except some categories CLogRouter => CFileLogRoute => categories => "*, ! Rate Topic: ***** 2 Votes

#1 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 17 June 2010 - 02:49 AM

Hi! im logging all erors and warnings, and sending to email. Problem is if someone, for example, accessing not existing page (404) , i get email. And yesterday google spider was trying to access robots.txt and i didn't have that. So i got 35 emails ;/ and one problem to resolve with message logging. As you understand problem is not in robots.txt file but in general. someone wants to get some page , it doesnt exist and it goes to email. not cool

and question is How can i ignore some cateories rom log router defined categories?
        'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array( 
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning', //logging all errors and warnings
                ),
                array(
                    'class'=>'CEmailLogRoute',
                    'levels'=>'error', // send to emails only errors
                    'email'=>'admin@example.com',
                    'categories'=>'!exception.CHttpException.404' // and here i want to put ignore statement to not send 404 errors
                ),
            ),
        ),


can i do that somehow?
or there i should do some workaround.
maybe i can remove " exception.CHttpException.* " from error category. this would resolve my problem as well.
-_-
0

#2 User is offline   Mike 

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

Posted 17 June 2010 - 03:47 AM

You could try to configure a custom log filter (which should extend CLogFilter). See source of CLogFilter::filter(&$logs). It should work, if you remove the unwanted entries from $logs there. Each $logs item is an array with these values:

array(string $message, integer $level, string $category, integer $time);

0

#3 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 17 June 2010 - 06:18 AM

I had a similar problem with favicon.ico.

Some firefox were trying to access to favicon in strange position, and there were lot of email.

I solved with a rewriteUrl that was sending to the proper favicon.ico whatever favicon.ico was requested.

Pay attention that "access to unexisting page" can even be an attack: someone can try to discover an admin module or other protected pages.

In my opinion is better to save all the log. Create your robot.txt (and that is a good stuff in general) and continue with the log as it is.
0

#4 User is offline   Mike 

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

Posted 17 June 2010 - 07:50 AM

@zaccaria:
You could still save all logs with CFileLogRoute. It's rather about what messages to filter out for CEmailLogRoute.
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 17 June 2010 - 08:26 AM

Yes, I know.

Was just a metodological question. As none read the log file, is better to send email for relevant notice.

I guess that this is why exist CEmailLogRouter, because people are reading emails much more often of how much are reading the log file.

So the questio is: what are log that worth to be sent via email? Of corse software failure are worth, because programmer can patch the problem before a customer call (I think that this is called 'proactivity' from 2.0 philosophs).

Should 'page not found' be sent via mail? In my opinion yes.

A page not find can be or a bug in the application (broken link) and so should be logged, or an address that a user manually entered, maybe with malicious ideas, and in this case should be also logged.

There are bot (or paranoid browser) that look for special files. In my opinion is better to place this few file and continue reciving all signalation.

Of corse is only an opinion, not a best practice.
0

#6 User is offline   Mike 

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

Posted 17 June 2010 - 08:57 AM

Too many emails cause that you ignore them after some time. I'd rather not get an email for each 404 as this is a too common error on any site. BTW you would make it very easy for an attacker to bomb your inbox ;).
0

#7 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 17 June 2010 - 09:46 AM

Thanks guys!
and Mike i took your advice and created new LogFilter from CLogFilter. If you are interested ill give what i got

My class
<?php
class LogFilter extends CLogFilter {
    public $ignoreCategories; // =array('category','category.*','some.category.tree.*');
    
    public function filter(&$logs)
    {                   
          // unset categories marked as "ignored"
          if($logs) foreach($logs as $logKey => $log) {
            $logCategory = $log[2]; //log category
            foreach($this->ignoreCategories as $nocat) {
                //exact match
                if($logCategory === $nocat) {
                    unset($logs[$logKey]);
                }
                //asterix match
                else if(strpos($nocat,'.*') !== false) {
                    $nocat = str_replace('.*','',$nocat).'.'; //remove asterix item from array and add dot at the and 
                    if(strpos($logCategory.'.',$nocat) !== false) {
                        unset($logs[$logKey]);
                    }
                    
                }
            }
            
          }

          $this->format($logs);
          
          return $logs;
    }
}


and configuration line in main.php

 'log'=>array(
            'class'=>'CLogRouter',
            'routes'=>array(
                array( 
                    'class'=>'CFileLogRoute',
                    'levels'=>'error, warning', //logging all errors and warnings
                ),
                array(
                    'class'=>'CEmailLogRoute',
                    'levels'=>'error', // send to emails only errors
                    'email'=>'admin@example.com',

		    'filter'=>array(
			'class'=>'LogFilter',
			'ignoreCategories'=>array(
			       'exception.CHttpException.*'
				/*,'some.category.*'*/
			)
		    ),

                ),
            ),
        ),


;)
-_-
3

#8 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 17 June 2010 - 10:19 AM

ahh, its now senging blank emails. cant disable logging even if $logs is empty
-_-
0

#9 User is offline   Mike 

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

Posted 18 June 2010 - 01:47 AM

*
POPULAR

Hmm. I'd say you could create a ticket for this. The way filters are implemented now, they are meant to format/decorate the log entries before sending. I think it makes sense to also enable filtering out a message with a filter. The fix would be a small change in CLogRoute::collectLogs().

Instead of:


    public function collectLogs($logger, $processLogs=false)
    {
        $logs=$logger->getLogs($this->levels,$this->categories);
        $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
        if($processLogs && !empty($this->logs))
        {
            if($this->filter!==null)
                Yii::createComponent($this->filter)->filter($this->logs);
            $this->processLogs($this->logs);
        }
    }



it needs to be:

    public function collectLogs($logger, $processLogs=false)
    {
        $logs=$logger->getLogs($this->levels,$this->categories);
        $this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
        if($processLogs && !empty($this->logs))
        {
            if($this->filter!==null)
                Yii::createComponent($this->filter)->filter($this->logs);

            if (!empty($this->logs))
                $this->processLogs($this->logs);
        }
    }



As a workaround you could extend CEmailLogRoute and override that method there.
5

#10 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 18 June 2010 - 02:28 AM

Yes, now its working, thanks again :) i sent ticket, too
-_-
0

#11 User is offline   Pentium10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 10-December 10

Posted 14 June 2011 - 12:40 AM

Where is that ticket located, I need it too.
0

#12 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 14 June 2011 - 01:28 AM

Sorry, cant find this ticket, but i think it was fixed (i didnt implement in my code, so not tested, yet) (bow)
-_-
0

#13 User is offline   Pentium10 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 10-December 10

Posted 14 June 2011 - 01:33 AM

It was not fixed, I am using the latest version and still empty logs.
0

#14 User is offline   briiC.lv 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 42
  • Joined: 25-April 09
  • Location:Latvia

Posted 14 June 2011 - 04:35 AM

View PostPentium10, on 14 June 2011 - 01:33 AM, said:

It was not fixed, I am using the latest version and still empty logs.

it looks like it has been deleted permanetly, because on http://code.google.c...ii/issues/entry
i couldn't find any reference to my ticket. Maybe add your ticket and post it here. I will vote for it, too.
-_-
0

#15 User is offline   SteveK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 24-June 11

Posted 29 September 2011 - 08:19 PM

*
POPULAR

Here's a complete example of filtering out / ignoring 404 errors based on narkomanC's filter. This will not require changes to the core files and will not send blank email alerts.

config/main.php
		'log' => array(
			'class' => 'CLogRouter',
			'routes' => array(
				array(
					'class' => 'CDbLogRoute',
					'logTableName' => 'yii_log',
					'connectionID' => 'db',
					'levels' => 'error, warning',
				),
				array(
					'class' => 'AdvancedEmailLogRoute',
					'filter' => array(
						'class'=>'AdvancedLogFilter',
						'ignoreCategories' => array(
							// Ignore 404s
							'exception.CHttpException.404',
						),
					),
					'levels' => 'error',
					'emails' => 'myemail@mydomain.com',
				),
			),
		),


protected/components/AdvancedLogFilter.php
<?php
class AdvancedLogFilter extends CLogFilter {
	public $ignoreCategories; // =array('category','category.*','some.category.tree.*');

	public function filter(&$logs) {
		// unset categories marked as "ignored"
		if($logs) foreach($logs as $logKey => $log) {
			$logCategory = $log[2]; //log category
			foreach($this->ignoreCategories as $nocat) {
				// Exact match
				if($logCategory === $nocat) {
					unset($logs[$logKey]);
					continue;
				}
				// Wildcard match
				else if(strpos($nocat,'.*') !== false) {
					$nocat = str_replace('.*','',$nocat).'.'; //remove asterix item from array and add dot at the and
					if(strpos($logCategory.'.',$nocat) !== false) {
						unset($logs[$logKey]);
					}
				}
			}
		}

		$this->format($logs);
		return $logs;
	}
}


protected/components/AdvancedEmailLogRoute.php
<?php
class AdvancedEmailLogRoute extends CEmailLogRoute {
	protected function processLogs($logs) {
		if (empty($logs)) {
			return;
		}
		parent::processLogs($logs);
	}
}

6

#16 User is offline   alisherdavronov 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 14-August 11

Posted 07 September 2012 - 09:36 AM

thanx
0

#17 User is offline   befi 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 10-March 11

Posted 28 January 2013 - 04:17 PM

exactly what I was looking for! Works like a charm!
0

#18 User is offline   Asterisk Pound 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 11
  • Joined: 18-September 10

Posted 21 March 2013 - 01:50 PM

This is great.

However I wanted to get the other CLogFilter options to work as well. So I added a couple lines of code...

here's my config.
<?php
            'routes'=>array(
                array(
                    'class'  => 'AdvancedEmailLogRoute',
                    'levels' => 'error, warning',
                    'emails' => 'support@mycompany.com',
                    'filter' => array(
                        'class'         => 'AdvancedLogFilter',
                        'prefixSession' => true,
                        'prefixUser'    => true,
                        'logUser'       => true,
                        // 'logVars' => array(),
                        'ignoreCategories' => array(
                            // Ignore 404s
                            'exception.CHttpException.404',
                        ),
                    ),
                ),



and I added the getContext back in.

<?php
class AdvancedLogFilter extends CLogFilter {
    public $ignoreCategories; // =array('category','category.*','some.category.tree.*');

    public function filter(&$logs) {
        // unset categories marked as "ignored"
        if($logs) foreach($logs as $logKey => $log) {
            $logCategory = $log[2]; //log category
            foreach($this->ignoreCategories as $nocat) {
                // Exact match
                if($logCategory === $nocat) {
                    unset($logs[$logKey]);
                    continue;
                }
                // Wildcard match
                else if(strpos($nocat,'.*') !== false) {
                    $nocat = str_replace('.*','',$nocat).'.'; //remove asterix item from array and add dot at the and
                    if(strpos($logCategory.'.',$nocat) !== false) {
                        unset($logs[$logKey]);
                    }
                }
            }
        }

        if (!empty($logs))
        {
            if(($message=$this->getContext())!=='')
                array_unshift($logs,array($message,CLogger::LEVEL_INFO,'application',YII_BEGIN_TIME));
            $this->format($logs);
        }
        $this->format($logs);
        return $logs;
    }
}


Enjoy
0

#19 User is offline   Revelis Luc Bonnin 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 18
  • Joined: 23-May 11
  • Location:Earth

Posted 03 May 2013 - 09:54 AM

excellent, thanks a lot !
I've added several logs on overloaded method processLogs() to be more accurate and it's just perfect !

View PostSteveK, on 29 September 2011 - 08:19 PM, said:

Here's a complete example of filtering out / ignoring 404 errors based on narkomanC's filter. This will not require changes to the core files and will not send blank email alerts.

config/main.php
		'log' => array(
			'class' => 'CLogRouter',
			'routes' => array(
				array(
					'class' => 'CDbLogRoute',
					'logTableName' => 'yii_log',
					'connectionID' => 'db',
					'levels' => 'error, warning',
				),
				array(
					'class' => 'AdvancedEmailLogRoute',
					'filter' => array(
						'class'=>'AdvancedLogFilter',
						'ignoreCategories' => array(
							// Ignore 404s
							'exception.CHttpException.404',
						),
					),
					'levels' => 'error',
					'emails' => 'myemail@mydomain.com',
				),
			),
		),


protected/components/AdvancedLogFilter.php
<?php
class AdvancedLogFilter extends CLogFilter {
	public $ignoreCategories; // =array('category','category.*','some.category.tree.*');

	public function filter(&$logs) {
		// unset categories marked as "ignored"
		if($logs) foreach($logs as $logKey => $log) {
			$logCategory = $log[2]; //log category
			foreach($this->ignoreCategories as $nocat) {
				// Exact match
				if($logCategory === $nocat) {
					unset($logs[$logKey]);
					continue;
				}
				// Wildcard match
				else if(strpos($nocat,'.*') !== false) {
					$nocat = str_replace('.*','',$nocat).'.'; //remove asterix item from array and add dot at the and
					if(strpos($logCategory.'.',$nocat) !== false) {
						unset($logs[$logKey]);
					}
				}
			}
		}

		$this->format($logs);
		return $logs;
	}
}


protected/components/AdvancedEmailLogRoute.php
<?php
class AdvancedEmailLogRoute extends CEmailLogRoute {
	protected function processLogs($logs) {
		if (empty($logs)) {
			return;
		}
		parent::processLogs($logs);
	}
}


0

#20 User is offline   Tanguy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 07-September 12

Posted 05 March 2014 - 05:44 AM

There is a property for exceptions:

			'class'=>'CLogRouter',
			'routes'=>array(
				array(
					'class'=>'CFileLogRoute',
					'levels'=>'error, warning',
					'except'=>'exception.CHttpException.404',
				),
				array(
					'class'=>'CFileLogRoute',
					'logFile'=>'404.log',
					'categories'=>'exception.CHttpException.404',
				),

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