Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/log-fingers_crossed' into develop
Browse files Browse the repository at this point in the history
Close #2271
  • Loading branch information
weierophinney committed Sep 18, 2012
2 parents e4864c6 + 576b759 commit 89997a1
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 8 deletions.
168 changes: 168 additions & 0 deletions library/Zend/Log/Writer/FingersCrossed.php
@@ -0,0 +1,168 @@
<?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;

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\WriterInterface;
use Zend\Log\Writer\AbstractWriter;

/**
* Buffers all events until the strategy determines to flush them.
*
* @see http://packages.python.org/Logbook/api/handlers.html#logbook.FingersCrossedHandler
* @category Zend
* @package Zend_Log
* @subpackage Writer
*/
class FingersCrossed extends AbstractWriter
{

/**
* The wrapped writer
*
* @var WriterInterface
*/
protected $writer;

/**
* Flag if buffering is enabled
*
* @var boolean
*/
protected $buffering = true;

/**
* Oldest entries are removed from the buffer if bufferSize is reached.
* 0 is infinte buffer size.
*
* @var int
*/
protected $bufferSize;

/**
* array of log events
*
* @var array
*/
protected $buffer = array();

/**
* Constructor
*
* @param WriterInterface $writer Wrapped writer
* @param FilterInterface|int $filterOrPriority Filter or log priority which determines buffering of events
* @param int $bufferSize Maximum buffer size
*/
public function __construct(WriterInterface $writer, $filterOrPriority = null, $bufferSize = 0)
{
$this->writer = $writer;

if (null === $filterOrPriority) {
$filterOrPriority = new PriorityFilter(Logger::WARN);
} elseif (!$filterOrPriority instanceof FilterInterface) {
$filterOrPriority = new PriorityFilter($filterOrPriority);
}

$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
*
* @param array $event event data
* @return void
*/
protected function doWrite(array $event)
{
if (!$this->buffering) {
$this->writer->write($event);
return;
}

$this->buffer[] = $event;

if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
array_shift($this->buffer);
}

if (!$this->isActivated($event)) {
return;
}

$this->buffering = false;

foreach ($this->buffer as $bufferedEvent) {
$this->writer->write($bufferedEvent);
}
}

/**
* Resets the state of the handler.
* Stops forwarding records to the wrapped writer
*/
public function reset()
{
$this->buffering = true;
}

/**
* Stub in accordance to parent method signature.
* Fomatters must be set on the wrapped writer.
*
* @param Formatter $formatter
*/
public function setFormatter(FormatterInterface $formatter)
{
return $this->writer;
}

/**
* Record shutdown
*
* @return void
*/
public function shutdown()
{
$this->writer->shutdown();
$this->buffer = null;
}
}
17 changes: 9 additions & 8 deletions library/Zend/Log/WriterPluginManager.php
Expand Up @@ -24,14 +24,15 @@ class WriterPluginManager extends AbstractPluginManager
* @var array
*/
protected $invokableClasses = array(
'db' => 'Zend\Log\Writer\Db',
'firephp' => 'Zend\Log\Writer\FirePhp',
'mail' => 'Zend\Log\Writer\Mail',
'mock' => 'Zend\Log\Writer\Mock',
'null' => 'Zend\Log\Writer\Null',
'stream' => 'Zend\Log\Writer\Stream',
'syslog' => 'Zend\Log\Writer\Syslog',
'zendmonitor' => 'Zend\Log\Writer\ZendMonitor',
'db' => 'Zend\Log\Writer\Db',
'fingerscrossed' => 'Zend\Log\Writer\FingersCrossed',
'firephp' => 'Zend\Log\Writer\FirePhp',
'mail' => 'Zend\Log\Writer\Mail',
'mock' => 'Zend\Log\Writer\Mock',
'null' => 'Zend\Log\Writer\Null',
'stream' => 'Zend\Log\Writer\Stream',
'syslog' => 'Zend\Log\Writer\Syslog',
'zendmonitor' => 'Zend\Log\Writer\ZendMonitor',
);

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/ZendTest/Log/Writer/FingersCrossedTest.php
@@ -0,0 +1,57 @@
<?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 ZendTest\Log\Writer;

use Zend\Log\Writer\FingersCrossed as FingersCrossedWriter;
use Zend\Log\Writer\Mock as MockWriter;
use Zend\Log\Logger;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class FingersCrossedTest extends \PHPUnit_Framework_TestCase
{
public function testBuffering()
{
$wrappedWriter = new MockWriter();
$writer = new FingersCrossedWriter($wrappedWriter, 2);

$writer->write(array('priority' => 3, 'message' => 'foo'));

$this->assertSame(count($wrappedWriter->events), 0);
}

public function testFlushing()
{
$wrappedWriter = new MockWriter();
$writer = new FingersCrossedWriter($wrappedWriter, 2);

$writer->write(array('priority' => 3, 'message' => 'foo'));
$writer->write(array('priority' => 1, 'message' => 'bar'));

$this->assertSame(count($wrappedWriter->events), 2);
}

public function testAfterFlushing()
{
$wrappedWriter = new MockWriter();
$writer = new FingersCrossedWriter($wrappedWriter, 2);

$writer->write(array('priority' => 3, 'message' => 'foo'));
$writer->write(array('priority' => 1, 'message' => 'bar'));
$writer->write(array('priority' => 3, 'message' => 'bar'));

$this->assertSame(count($wrappedWriter->events), 3);
}
}

0 comments on commit 89997a1

Please sign in to comment.