3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Log\Filter
;
13 use Zend\Log\Exception
;
14 use Zend\Stdlib\ErrorHandler
;
16 class Regex
implements FilterInterface
26 * Filter out any log messages not matching the pattern
28 * @param string|array|Traversable $regex Regular expression to test the log message
30 * @throws Exception\InvalidArgumentException
32 public function __construct($regex)
34 if ($regex instanceof Traversable
) {
35 $regex = iterator_to_array($regex);
37 if (is_array($regex)) {
38 $regex = isset($regex['regex']) ?
$regex['regex'] : null;
40 ErrorHandler
::start(E_WARNING
);
41 $result = preg_match($regex, '');
42 $error = ErrorHandler
::stop();
43 if ($result === false) {
44 throw new Exception\
InvalidArgumentException(sprintf(
45 'Invalid regular expression "%s"',
49 $this->regex
= $regex;
53 * Returns TRUE to accept the message, FALSE to block it.
55 * @param array $event event data
56 * @return bool accepted?
58 public function filter(array $event)
60 $message = $event['message'];
61 if (is_array($event['message'])) {
62 $message = var_export($message, TRUE);
64 return preg_match($this->regex
, $message) > 0;