mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-01 19:00:20 +02:00
Add support for PSR-3 levels to all public APIs accepting levels, fixes #421
This commit is contained in:
@@ -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
|
||||
-----
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
|
@@ -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)
|
||||
|
@@ -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)
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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
|
||||
*
|
||||
|
@@ -26,7 +26,7 @@ class GitProcessor
|
||||
|
||||
public function __construct($level = Logger::DEBUG)
|
||||
{
|
||||
$this->level = $level;
|
||||
$this->level = Logger::toMonologLevel($level);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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;
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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
|
||||
*/
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user