1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 04:37:38 +02:00

Fix a few details and add docs to the ChannelLevelActivationStrategy, refs #186

This commit is contained in:
Jordi Boggiano
2013-07-17 18:10:31 +02:00
parent e9df30c7f5
commit 050bb52fd8
3 changed files with 26 additions and 5 deletions

View File

@@ -16,6 +16,19 @@ namespace Monolog\Handler\FingersCrossed;
* based on level per channel. e.g. trigger activation on level 'ERROR' by default, except * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except
* for records of the 'sql' channel; those should trigger activation on level 'WARN'. * for records of the 'sql' channel; those should trigger activation on level 'WARN'.
* *
* Example:
*
* <code>
* $activationStrategy = new ChannelLevelActivationStrategy(
* Logger::CRITICAL,
* array(
* 'request' => Logger::ALERT,
* 'sensitive' => Logger::ERROR,
* )
* );
* $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy);
* </code>
*
* @author Mike Meessen <netmikey@gmail.com> * @author Mike Meessen <netmikey@gmail.com>
*/ */
class ChannelLevelActivationStrategy implements ActivationStrategyInterface class ChannelLevelActivationStrategy implements ActivationStrategyInterface
@@ -37,8 +50,8 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface
{ {
if (isset($this->channelToActionLevel[$record['channel']])) { if (isset($this->channelToActionLevel[$record['channel']])) {
return $record['level'] >= $this->channelToActionLevel[$record['channel']]; return $record['level'] >= $this->channelToActionLevel[$record['channel']];
} else {
return $record['level'] >= $this->defaultActionLevel;
} }
return $record['level'] >= $this->defaultActionLevel;
} }
} }

View File

@@ -22,6 +22,9 @@ use Monolog\Logger;
* Only requests which actually trigger an error (or whatever your actionLevel is) will be * Only requests which actually trigger an error (or whatever your actionLevel is) will be
* in the logs, but they will contain all records, not only those above the level threshold. * in the logs, but they will contain all records, not only those above the level threshold.
* *
* You can find the various activation strategies in the
* Monolog\Handler\FingersCrossed\ namespace.
*
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class FingersCrossedHandler extends AbstractHandler class FingersCrossedHandler extends AbstractHandler
@@ -45,6 +48,8 @@ class FingersCrossedHandler extends AbstractHandler
if (null === $activationStrategy) { if (null === $activationStrategy) {
$activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING); $activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING);
} }
// convert simple int activationStrategy to an object
if (!$activationStrategy instanceof ActivationStrategyInterface) { if (!$activationStrategy instanceof ActivationStrategyInterface) {
$activationStrategy = new ErrorLevelActivationStrategy($activationStrategy); $activationStrategy = new ErrorLevelActivationStrategy($activationStrategy);
} }

View File

@@ -138,8 +138,10 @@ class FingersCrossedHandlerTest extends TestCase
/** /**
* @covers Monolog\Handler\FingersCrossedHandler::__construct * @covers Monolog\Handler\FingersCrossedHandler::__construct
* @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct
* @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated
*/ */
public function testActivationStrategy() public function testErrorLevelActivationStrategy()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING)); $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING));
@@ -151,9 +153,10 @@ class FingersCrossedHandlerTest extends TestCase
} }
/** /**
* @covers Monolog\Handler\FingersCrossedHandler::__construct * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct
* @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated
*/ */
public function testCategoryErrorLevelActivationStrategy() public function testChannelLevelActivationStrategy()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG))); $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG)));