diff --git a/src/Filter/Sample.php b/src/Filter/Sample.php new file mode 100644 index 00000000..ae0c8882 --- /dev/null +++ b/src/Filter/Sample.php @@ -0,0 +1,57 @@ +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; + } +} diff --git a/src/LoggerAwareInterface.php b/src/LoggerAwareInterface.php index f0c720f9..028c7f40 100644 --- a/src/LoggerAwareInterface.php +++ b/src/LoggerAwareInterface.php @@ -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(); } diff --git a/src/LoggerAwareTrait.php b/src/LoggerAwareTrait.php index 70152481..2ed7c00c 100644 --- a/src/LoggerAwareTrait.php +++ b/src/LoggerAwareTrait.php @@ -30,4 +30,14 @@ public function setLogger(LoggerInterface $logger) return $this; } + + /** + * Get logger object + * + * @return null|LoggerInterface + */ + public function getLogger() + { + return $this->logger; + } } diff --git a/src/Processor/Backtrace.php b/src/Processor/Backtrace.php index e827bbfc..9a3cacb2 100644 --- a/src/Processor/Backtrace.php +++ b/src/Processor/Backtrace.php @@ -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); } diff --git a/src/Processor/RequestId.php b/src/Processor/RequestId.php index 61520b21..5cfcb927 100644 --- a/src/Processor/RequestId.php +++ b/src/Processor/RequestId.php @@ -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']; diff --git a/test/Filter/SampleTest.php b/test/Filter/SampleTest.php new file mode 100644 index 00000000..56972288 --- /dev/null +++ b/test/Filter/SampleTest.php @@ -0,0 +1,62 @@ +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); + } +}