diff --git a/README.mdown b/README.mdown index 9054b8e6..43d9bcfc 100644 --- a/README.mdown +++ b/README.mdown @@ -14,6 +14,8 @@ This library implements the [PSR-3](https://github.com/php-fig/fig-standards/blo interface that you can type-hint against in your own libraries to keep a maximum of interoperability. You can also use it in your applications to make sure you can always use another compatible logger at a later time. +As of 1.11.0 Monolog public APIs will also accept PSR-3 log levels. +Internally Monolog still uses its own level scheme since it predates PSR-3. Usage ----- diff --git a/src/Monolog/Handler/AbstractHandler.php b/src/Monolog/Handler/AbstractHandler.php index 3bb21b71..69ede49a 100644 --- a/src/Monolog/Handler/AbstractHandler.php +++ b/src/Monolog/Handler/AbstractHandler.php @@ -37,7 +37,7 @@ abstract class AbstractHandler implements HandlerInterface */ public function __construct($level = Logger::DEBUG, $bubble = true) { - $this->level = $level; + $this->setLevel($level); $this->bubble = $bubble; } @@ -123,7 +123,7 @@ abstract class AbstractHandler implements HandlerInterface */ public function setLevel($level) { - $this->level = $level; + $this->level = Logger::toMonologLevel($level); return $this; } diff --git a/src/Monolog/Handler/FilterHandler.php b/src/Monolog/Handler/FilterHandler.php index 96cc7286..8f4d2a07 100644 --- a/src/Monolog/Handler/FilterHandler.php +++ b/src/Monolog/Handler/FilterHandler.php @@ -63,11 +63,13 @@ class FilterHandler extends AbstractHandler public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY) { if (is_array($minLevelOrList)) { - $acceptedLevels = $minLevelOrList; + $acceptedLevels = array_map('Monolog\Logger::toMonologLevel', $minLevelOrList); } else { - $acceptedLevels = array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) { + $minLevelOrList = Logger::toMonologLevel($minLevelOrList); + $maxLevel = Logger::toMonologLevel($maxLevel); + $acceptedLevels = array_values(array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) { return $level >= $minLevelOrList && $level <= $maxLevel; - }); + })); } $this->acceptedLevels = array_flip($acceptedLevels); } diff --git a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index ae702e45..e3b403f6 100644 --- a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -11,6 +11,8 @@ namespace Monolog\Handler\FingersCrossed; +use Monolog\Logger; + /** * Channel and Error level based monolog activation strategy. Allows to trigger activation * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except @@ -42,8 +44,8 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface */ public function __construct($defaultActionLevel, $channelToActionLevel = array()) { - $this->defaultActionLevel = $defaultActionLevel; - $this->channelToActionLevel = $channelToActionLevel; + $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel); + $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel); } public function isHandlerActivated(array $record) diff --git a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php index 7cd8ef1b..6e630852 100644 --- a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php @@ -11,6 +11,8 @@ namespace Monolog\Handler\FingersCrossed; +use Monolog\Logger; + /** * Error level based activation strategy. * @@ -22,7 +24,7 @@ class ErrorLevelActivationStrategy implements ActivationStrategyInterface public function __construct($actionLevel) { - $this->actionLevel = $actionLevel; + $this->actionLevel = Logger::toMonologLevel($actionLevel); } public function isHandlerActivated(array $record) diff --git a/src/Monolog/Handler/PushoverHandler.php b/src/Monolog/Handler/PushoverHandler.php index e9f78435..0dc2e27e 100644 --- a/src/Monolog/Handler/PushoverHandler.php +++ b/src/Monolog/Handler/PushoverHandler.php @@ -65,8 +65,8 @@ class PushoverHandler extends SocketHandler $this->token = $token; $this->users = (array) $users; $this->title = $title ?: gethostname(); - $this->highPriorityLevel = $highPriorityLevel; - $this->emergencyLevel = $emergencyLevel; + $this->highPriorityLevel = Logger::toMonologLevel($highPriorityLevel); + $this->emergencyLevel = Logger::toMonologLevel($emergencyLevel); $this->retry = $retry; $this->expire = $expire; } diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 3e3f95ce..2ece2894 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -388,6 +388,21 @@ class Logger implements LoggerInterface return static::$levels[$level]; } + /** + * Converts PSR-3 levels to Monolog ones if necessary + * + * @param string|int Level number (monolog) or name (PSR-3) + * @return int + */ + public static function toMonologLevel($level) + { + if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) { + return constant(__CLASS__.'::'.strtoupper($level)); + } + + return $level; + } + /** * Checks whether the Logger has a handler that listens on the given level * diff --git a/src/Monolog/Processor/GitProcessor.php b/src/Monolog/Processor/GitProcessor.php index 96c2abfe..1899400d 100644 --- a/src/Monolog/Processor/GitProcessor.php +++ b/src/Monolog/Processor/GitProcessor.php @@ -26,7 +26,7 @@ class GitProcessor public function __construct($level = Logger::DEBUG) { - $this->level = $level; + $this->level = Logger::toMonologLevel($level); } /** diff --git a/src/Monolog/Processor/IntrospectionProcessor.php b/src/Monolog/Processor/IntrospectionProcessor.php index a2c48ce6..294a295c 100644 --- a/src/Monolog/Processor/IntrospectionProcessor.php +++ b/src/Monolog/Processor/IntrospectionProcessor.php @@ -32,7 +32,7 @@ class IntrospectionProcessor public function __construct($level = Logger::DEBUG, array $skipClassesPartials = array('Monolog\\')) { - $this->level = $level; + $this->level = Logger::toMonologLevel($level); $this->skipClassesPartials = $skipClassesPartials; } diff --git a/tests/Monolog/Handler/AbstractHandlerTest.php b/tests/Monolog/Handler/AbstractHandlerTest.php index 01d522fa..568eb9da 100644 --- a/tests/Monolog/Handler/AbstractHandlerTest.php +++ b/tests/Monolog/Handler/AbstractHandlerTest.php @@ -62,6 +62,17 @@ class AbstractHandlerTest extends TestCase $this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG))); } + /** + * @covers Monolog\Handler\AbstractHandler::__construct + */ + public function testHandlesPsrStyleLevels() + { + $handler = $this->getMockForAbstractClass('Monolog\Handler\AbstractHandler', array('warning', false)); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG))); + $handler->setLevel('debug'); + $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG))); + } + /** * @covers Monolog\Handler\AbstractHandler::getFormatter * @covers Monolog\Handler\AbstractHandler::getDefaultFormatter diff --git a/tests/Monolog/Handler/FilterHandlerTest.php b/tests/Monolog/Handler/FilterHandlerTest.php index 1f86c8ec..76253c16 100644 --- a/tests/Monolog/Handler/FilterHandlerTest.php +++ b/tests/Monolog/Handler/FilterHandlerTest.php @@ -80,6 +80,16 @@ class FilterHandlerTest extends TestCase $levels = array(Logger::INFO, Logger::ERROR); $handler->setAcceptedLevels($levels); $this->assertSame($levels, $handler->getAcceptedLevels()); + + $handler->setAcceptedLevels(array('info', 'error')); + $this->assertSame($levels, $handler->getAcceptedLevels()); + + $levels = array(Logger::CRITICAL, Logger::ALERT, Logger::EMERGENCY); + $handler->setAcceptedLevels(Logger::CRITICAL, Logger::EMERGENCY); + $this->assertSame($levels, $handler->getAcceptedLevels()); + + $handler->setAcceptedLevels('critical', 'emergency'); + $this->assertSame($levels, $handler->getAcceptedLevels()); } /** diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index d02fef99..a3d350d5 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -156,6 +156,22 @@ class FingersCrossedHandlerTest extends TestCase $this->assertTrue($test->hasWarningRecords()); } + /** + * @covers Monolog\Handler\FingersCrossedHandler::__construct + * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::__construct + * @covers Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy::isHandlerActivated + */ + public function testErrorLevelActivationStrategyWithPsrLevel() + { + $test = new TestHandler(); + $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning')); + $handler->handle($this->getRecord(Logger::DEBUG)); + $this->assertFalse($test->hasDebugRecords()); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasWarningRecords()); + } + /** * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated @@ -173,6 +189,23 @@ class FingersCrossedHandlerTest extends TestCase $this->assertTrue($test->hasWarningRecords()); } + /** + * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct + * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated + */ + public function testChannelLevelActivationStrategyWithPsrLevels() + { + $test = new TestHandler(); + $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy('error', array('othertest' => 'debug'))); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertFalse($test->hasWarningRecords()); + $record = $this->getRecord(Logger::DEBUG); + $record['channel'] = 'othertest'; + $handler->handle($record); + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasWarningRecords()); + } + /** * @covers Monolog\Handler\FingersCrossedHandler::handle */ diff --git a/tests/Monolog/Handler/NewRelicHandlerTest.php b/tests/Monolog/Handler/NewRelicHandlerTest.php index 3f3c07e2..73c2a53e 100644 --- a/tests/Monolog/Handler/NewRelicHandlerTest.php +++ b/tests/Monolog/Handler/NewRelicHandlerTest.php @@ -13,7 +13,6 @@ namespace Monolog\Handler; use Monolog\TestCase; use Monolog\Logger; -use Psr\Log\LogLevel; class NewRelicHandlerTest extends TestCase { @@ -85,7 +84,7 @@ class NewRelicHandlerTest extends TestCase public function testTheAppNameCanBeInjectedFromtheConstructor() { - $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName'); + $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName'); $handler->handle($this->getRecord(Logger::ERROR, 'log message')); $this->assertEquals('myAppName', self::$appname); @@ -93,7 +92,7 @@ class NewRelicHandlerTest extends TestCase public function testTheAppNameCanBeOverriddenFromEachLog() { - $handler = new StubNewRelicHandler(LogLevel::ALERT, false, 'myAppName'); + $handler = new StubNewRelicHandler(Logger::DEBUG, false, 'myAppName'); $handler->handle($this->getRecord(Logger::ERROR, 'log message', array('appname' => 'logAppName'))); $this->assertEquals('logAppName', self::$appname);