Permalink
Browse files

Replaced ActivationStrategy with filters.

Removed exception when setting formatter.
  • Loading branch information...
1 parent 1c54d2d commit d960eb6eacacd84405ee31382cd3fe666ff14b14 @stefankleff stefankleff committed Sep 10, 2012
@@ -9,11 +9,11 @@
*/
namespace Zend\Log\Writer;
+use Zend\Log\Filter\Priority as PriorityFilter;
+use Zend\Log\Filter\FilterInterface;
use Zend\Log\Formatter\FormatterInterface;
use Zend\Log\Exception;
use Zend\Log\Logger;
-use Zend\Log\Writer\FingersCrossed\ErrorLevelActivationStrategy;
-use Zend\Log\Writer\FingersCrossed\ActivationStrategyInterface;
use Zend\Log\Writer\WriterInterface;
use Zend\Log\Writer\AbstractWriter;
@@ -56,35 +56,55 @@ class FingersCrossed extends AbstractWriter
*/
protected $buffer = array();
- /**
- * Strategy which determines if events are buffered
- *
- * @var ActivationStrategyInterface
- */
- protected $activationStrategy;
-
/**
* Constructor
*
* @param WriterInterface $writer Wrapped writer
- * @param ActivationStrategyInterface|int $activationStrategyOrPriority Strategy or log priority which determines buffering of events
+ * @param FilterInterface|int $filterOrPriority Filter or log priority which determines buffering of events
* @param int $bufferSize Maximum buffer size
*/
- public function __construct(WriterInterface $writer, $activationStrategyOrPriority = null, $bufferSize = 0)
+ public function __construct(WriterInterface $writer, $filterOrPriority = null, $bufferSize = 0)
{
$this->writer = $writer;
- if ($activationStrategyOrPriority === null) {
- $this->activationStrategy = new ErrorLevelActivationStrategy(Logger::WARN);
- } elseif (! $activationStrategyOrPriority instanceof ActivationStrategyInterface) {
- $this->activationStrategy = new ErrorLevelActivationStrategy($activationStrategyOrPriority);
+ if (null === $filterOrPriority) {
+ $this->addFilter(new PriorityFilter(Logger::WARN));
+ } elseif (!$filterOrPriority instanceof FilterInterface) {
+ $this->addFilter(new PriorityFilter($filterOrPriority));
} else {
- $this->activationStrategy = $activationStrategyOrPriority;
+ $this->addFilter($filterOrPriority);
}
$this->bufferSize = $bufferSize;
}
+ /**
+ * Log a message to this writer.
+ *
+ * @param array $event log data event
+ * @return void
+ */
+ public function write(array $event)
+ {
+ $this->doWrite($event);
+ }
+
+ /**
+ * Check if buffered data should be flushed
+ *
+ * @param array $event event data
+ * @return boolean true if buffered data should be flushed
+ */
+ protected function isActivated(array $event)
+ {
+ foreach ($this->filters as $filter) {
+ if (!$filter->filter($event)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
/**
* Write message to buffer or delegate event data to the wrapped writer
*
@@ -100,7 +120,7 @@ protected function doWrite(array $event)
array_shift($this->buffer);
}
- if ($this->activationStrategy->isWriterActivated($event)) {
+ if ($this->isActivated($event)) {
$this->buffering = false;
foreach ($this->buffer as $bufferedEvent) {
@@ -122,13 +142,14 @@ public function reset()
}
/**
- * Prevent setting a formatter for this writer
+ * Stub in accordance to parent method signature.
+ * Fomatters must be set on the wrapped writer.
*
* @param Formatter $formatter
*/
public function setFormatter(FormatterInterface $formatter)
{
- throw new Exception\InvalidArgumentException('Formatter must be set on the wrapped writer');
+ return $this->writer;
}
/**
@@ -1,28 +0,0 @@
-<?php
-/**
- * Zend Framework (http://framework.zend.com/)
- *
- * @link http://github.com/zendframework/zf2 for the canonical source repository
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @package Zend_Log
- */
-
-namespace Zend\Log\Writer\FingersCrossed;
-
-/**
- *
- * @category Zend
- * @package Zend_Log
- * @subpackage Writer
- */
-interface ActivationStrategyInterface
-{
- /**
- * Returns whether the given record activates the writer.
- *
- * @param array event data
- * @return bool
- */
- public function isWriterActivated(array $event);
-}
@@ -1,45 +0,0 @@
-<?php
-/**
- * Zend Framework (http://framework.zend.com/)
- *
- * @link http://github.com/zendframework/zf2 for the canonical source repository
- * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @package Zend_Log
- */
-namespace Zend\Log\Writer\FingersCrossed;
-
-use Zend\Log\Logger;
-
-/**
- *
- * @category Zend
- * @package Zend_Log
- * @subpackage Writer
- */
-class ErrorLevelActivationStrategy implements ActivationStrategyInterface
-{
-
- protected $priority;
-
- /**
- * Constructor
- *
- * @param int $priority any event priority equals or severe than this will deactivate buffering
- */
- public function __construct($priority = Logger::WARN)
- {
- $this->priority = $priority;
- }
-
- /**
- * Returns whether the given record activates the writer
- *
- * @param array event data
- * @return bool
- */
- public function isWriterActivated(array $event)
- {
- return $event['priority'] <= $this->priority;
- }
-}

0 comments on commit d960eb6

Please sign in to comment.