Allow Negating Categories in CLogRoute #1376

Merged
merged 6 commits into from Sep 28, 2012
@@ -39,9 +39,15 @@
*/
public $levels='';
/**
- * @var string list of categories separated by comma or space. Defaults to empty, meaning all categories.
+ * @var mixed array of categories, or string list separated by comma or space.
+ * Defaults to empty, meaning all categories.
*/
- public $categories='';
+ public $categories=array();
+ /**
+ * @var mixed array of categories, or string list separated by comma or space, to EXCLUDE from logs.
+ * This will exclude any categories after $categories has been ran
+ */
+ public $except=array();
/**
* @var mixed the additional filter (eg {@link CLogFilter}) that can be applied to the log messages.
* The value of this property will be passed to {@link Yii::createComponent} to create
@@ -87,7 +93,7 @@ protected function formatLogMessage($message,$level,$category,$time)
*/
public function collectLogs($logger, $processLogs=false)
{
- $logs=$logger->getLogs($this->levels,$this->categories);
+ $logs=$logger->getLogs($this->levels,$this->categories,$this->except);
$this->logs=empty($this->logs) ? $logs : array_merge($this->logs,$logs);
if($processLogs && !empty($this->logs))
{
@@ -69,6 +69,10 @@ class CLogger extends CComponent
* @var array log categories for filtering (used when filtering)
*/
private $_categories;
+ /**
+ * @var array log categories for excluding from filtering (used when filtering)
+ */
+ private $_except;
/**
* @var array the profiling results (category, token => time in seconds)
*/
@@ -125,21 +129,29 @@ public function log($message,$level='info',$category='application')
* [2] => category (string)
* [3] => timestamp (float, obtained by microtime(true));
*/
- public function getLogs($levels='',$categories='')
+ public function getLogs($levels='',$categories=array(), $except=array())
{
$this->_levels=preg_split('/[\s,]+/',strtolower($levels),-1,PREG_SPLIT_NO_EMPTY);
- $this->_categories=preg_split('/[\s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
- if(empty($levels) && empty($categories))
- return $this->_logs;
- else if(empty($levels))
- return array_values(array_filter($this->_logs,array($this,'filterByCategory')));
- else if(empty($categories))
- return array_values(array_filter($this->_logs,array($this,'filterByLevel')));
+
+ if (is_string($categories))
+ $this->_categories=preg_split('/[\s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
else
- {
- $ret=array_filter($this->_logs,array($this,'filterByLevel'));
- return array_values(array_filter($ret,array($this,'filterByCategory')));
- }
+ $this->_categories=array_filter(array_map('strtolower',$categories));
@mdomba

mdomba Sep 13, 2012

Member

whitespace ?

@joshrp

joshrp Sep 13, 2012

Contributor

I'm not sure why it's displaying like that. I run netbeans and it's displaying proper (4 space wide) tab characters there.

Although it is showing the removals as 2 space characters too. is this a github display discrepancy?

@mdomba

mdomba Sep 13, 2012

Member

sorry.. all OK... this lines gets just under else... so it's properly indented :)

+
+ if (is_string($except))
+ $this->_except=preg_split('/[\s,]+/',strtolower($except),-1,PREG_SPLIT_NO_EMPTY);
+ else
+ $this->_except=array_filter(array_map('strtolower',$except));
+
+ $ret=$this->_logs;
+
+ if(!empty($levels))
+ $ret=array_values(array_filter($ret,array($this,'filterByLevel')));
+
+ if(!empty($this->_categories) || !empty($this->_except))
+ $ret=array_values(array_filter($ret,array($this,'filterByCategory')));
+
+ return $ret;
}
/**
@@ -149,13 +161,7 @@ public function getLogs($levels='',$categories='')
*/
private function filterByCategory($value)
{
- foreach($this->_categories as $category)
- {
- $cat=strtolower($value[2]);
- if($cat===$category || (($c=rtrim($category,'.*'))!==$category && strpos($cat,$c)===0))
- return true;
- }
- return false;
+ return $this->filterAllCategories($value, 2);
}
/**
@@ -165,13 +171,33 @@ private function filterByCategory($value)
*/
private function filterTimingByCategory($value)
{
+ return $this->filterAllCategories($value, 1);
+ }
+
+ /**
+ * Filter function used to filter included and excluded categories
+ * @param array $value element to be filtered
+ * @param integer index of the values array to be used for check
+ * @return boolean true if valid timing entry, false if not.
+ */
+ private function filterAllCategories($value, $index)
+ {
+ $cat=strtolower($value[$index]);
+ $ret=empty($this->_categories);
foreach($this->_categories as $category)
- {
- $cat=strtolower($value[1]);
+ {
if($cat===$category || (($c=rtrim($category,'.*'))!==$category && strpos($cat,$c)===0))
- return true;
+ $ret=true;
+ }
+ if ($ret)
+ {
+ foreach($this->_except as $category)
+ {
+ if($cat===$category || (($c=rtrim($category,'.*'))!==$category && strpos($cat,$c)===0))
+ $ret=false;
+ }
}
- return false;
+ return $ret;
}
/**