Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More log writer filters #657

Open
wants to merge 4 commits into
base: 3.4/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions classes/Kohana/Log/Filter/Body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Filter out logs by the body of the log message using a regex pattern
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_Filter_Body implements Kohana_Log_Filter {

/**
* @var a regex pattern to test log entries' bodies against
*/
private $regex_pattern;

public function __construct($regex_pattern)
{
if ( ! is_string($regex_pattern))
{
throw new InvalidArgumentException('Argument 1 of the constructor of Log_Filter_Body must be a string');
}
$this->regex_pattern = $regex_pattern;
}

public function process(array $messages)
{
$filtered = array();

foreach ($messages as $message)
{
if (preg_match($this->regex_pattern, $message['body']))
{
$filtered[] = $message;
}
}

return $filtered;
}

}
46 changes: 46 additions & 0 deletions classes/Kohana/Log/Filter/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Filter out logs when they are generated through an exception
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_Filter_Exception implements Kohana_Log_Filter {

/**
* @var Exception class name to filter log entries against
*/
private $exception;

public function __construct($exception = 'Exception')
{
if (!is_string($exception))
{
throw new InvalidArgumentException('Argument 1 of the constructor of Log_Filter_Exception must be a string');
}
$this->exception = $exception;
}

public function process(array $messages)
{
$filtered = array();

foreach ($messages as $message)
{
if (
(isset($message['exception']))
AND
($message['exception'] instanceof $this->exception)
)
{
$filtered[] = $message;
}
}

return $filtered;
}

}
42 changes: 42 additions & 0 deletions classes/Kohana/Log/Filter/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Filter out logs by the originating file using a regex pattern
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_Filter_File implements Kohana_Log_Filter {

/**
* @var a regex pattern to test log entries' originating file against
*/
private $regex_pattern;

public function __construct($regex_pattern)
{
if ( ! is_string($regex_pattern))
{
throw new InvalidArgumentException('Argument 1 of the constructor of Log_Filter_File must be a string');
}
$this->regex_pattern = $regex_pattern;
}

public function process(array $messages)
{
$filtered = array();

foreach ($messages as $message)
{
if (preg_match($this->regex_pattern, $message['file']))
{
$filtered[] = $message;
}
}

return $filtered;
}

}
68 changes: 68 additions & 0 deletions classes/Kohana/Log/Filter/Union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* A log filter that accepts other filters and make a union (OR logic) of logs
*
* @package Kohana
* @category Logging
* @author Kohana Team
* @copyright (c) 2008-2012 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Log_Filter_Union implements Kohana_Log_Filter_Aware, Kohana_Log_Filter {

/**
* @var array of Kohana_Log_Filter
*/
private $filters;

public function __construct(array $filters)
{
foreach ($filters as $filter)
{
$this->attach_filter($filter);
}
}

public function process(array $messages)
{
$filtered = array();

foreach ($this->filters as $filter)
{
$filtered[] = $filter->process($messages);
}

return array_unique($filtered);
}

/**
* Ataches a log filter
*
* @param Kohana_Log_Filter $filter
* @return Log_Filter_Union
*/
public function attach_filter(Kohana_Log_Filter $filter)
{
$this->filters[spl_object_hash($filter)] = $filter;

return $this;
}

/**
* Detaches a log filter
*
* @param Kohana_Log_Filter $filter
* @return Log_Filter_Union
*/
public function detach_filter(Kohana_Log_Filter $filter)
{
unset($this->filters[spl_object_hash($filter)]);

return $this;
}

public function filter(array $messages)
{
return $this->process($messages);
}
}
3 changes: 3 additions & 0 deletions classes/Log/Filter/Body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class Log_Filter_Body extends Kohana_Log_Filter_Body {}
3 changes: 3 additions & 0 deletions classes/Log/Filter/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class Log_Filter_Exception extends Kohana_Log_Filter_Exception {}
3 changes: 3 additions & 0 deletions classes/Log/Filter/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class Log_Filter_File extends Kohana_Log_Filter_File {}
3 changes: 3 additions & 0 deletions classes/Log/Filter/Union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class Log_Filter_Union extends Kohana_Log_Filter_Union {}