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

Commit

Permalink
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 3 deletions.
57 changes: 57 additions & 0 deletions src/Filter/Sample.php
Original file line number Diff line number Diff line change
@@ -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-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Log\Filter;

use Zend\Log\Exception;
use Zend\Log\Filter\FilterInterface;

class Sample implements FilterInterface
{
/**
* Sample rate [0-1].
*
* @var float
*/
protected $sampleRate;

/**
* Filters logging by sample rate.
*
* Sample rate must be a float number between 0 and 1 included.
* If 0.5, only half of the values will be logged.
* If 0.1 only 1 among 10 values will be logged.
*
* @param float|int $samplerate Sample rate [0-1].
* @return Priority
* @throws Exception\InvalidArgumentException
*/
public function __construct($sampleRate = 1)
{
if (! is_numeric($sampleRate)) {
throw new Exception\InvalidArgumentException(sprintf(
'Sample rate must be numeric, received "%s"',
gettype($sampleRate)
));
}

$this->sampleRate = (float) $sampleRate;
}

/**
* Returns TRUE to accept the message, FALSE to block it.
*
* @param array $event event data
* @return bool Accepted ?
*/
public function filter(array $event)
{
return (mt_rand() / mt_getrandmax()) <= $this->sampleRate;
}
}
14 changes: 14 additions & 0 deletions src/LoggerAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,19 @@
*/
interface LoggerAwareInterface
{
/**
* Set logger instance
*
* @param LoggerInterface
* @return void
*/
public function setLogger(LoggerInterface $logger);

/**
* Get logger instance. Currently commented out as this would possibly break
* existing implementations.
*
* @return null|LoggerInterface
*/
// public function getLogger();
}
10 changes: 10 additions & 0 deletions src/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public function setLogger(LoggerInterface $logger)

return $this;
}

/**
* Get logger object
*
* @return null|LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}
}
4 changes: 2 additions & 2 deletions src/Processor/Backtrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ public function process(array $event)
*/
protected function getBacktrace()
{
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
if (PHP_VERSION_ID >= 50400) {
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, $this->traceLimit);
}

if (version_compare(PHP_VERSION, '5.3.6') >= 0) {
if (PHP_VERSION_ID >= 50306) {
return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Processor/RequestId.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected function getIdentifier()
return $this->identifier;
}

$requestTime = (version_compare(PHP_VERSION, '5.4.0') >= 0)
$requestTime = (PHP_VERSION_ID >= 50400)
? $_SERVER['REQUEST_TIME_FLOAT']
: $_SERVER['REQUEST_TIME'];

Expand Down
62 changes: 62 additions & 0 deletions test/Filter/SampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 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\Filter;

use Zend\Log\Filter\Sample;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class SampleTest extends \PHPUnit_Framework_TestCase
{
public function testConstructorThrowsOnInvalidSampleRate()
{
$this->setExpectedException('Zend\Log\Exception\InvalidArgumentException', 'must be numeric');
new Sample('bar');
}

public function testSampleLimit0()
{
// Should log nothing.
$filter = new Sample(0);

// Since sampling is a random process, let's test several times.
$ret = false;
for ($i = 0; $i < 100; $i ++) {
if ($filter->filter(array())) {
break;
$ret = true;
}
}

$this->assertFalse($ret);
}

public function testSampleLimit1()
{
// Should log all events.
$filter = new Sample(1);

// Since sampling is a random process, let's test several times.
$ret = true;
for ($i = 0; $i < 100; $i ++) {
if (! $filter->filter(array())) {
break;
$ret = false;
}
}

$this->assertTrue($ret);
}
}

0 comments on commit 354da3a

Please sign in to comment.