1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-07 05:36:45 +02:00

#508 add hasLogger method to Monolog\Registry

This commit is contained in:
dtarasov
2015-02-18 11:56:56 +03:00
parent f05ed3a44a
commit ad5d65b262
2 changed files with 79 additions and 0 deletions

View File

@@ -63,6 +63,21 @@ class Registry
self::$loggers[$name] = $logger;
}
/**
* Checks if such logging channel exists by name or instance
*
* @param string|Logger $logger Name or logger instance
*/
public static function hasLogger($logger)
{
if ($logger instanceof Logger) {
$index = array_search($logger, self::$loggers, true);
return false !== $index;
} else {
return isset(self::$loggers[$logger]);
}
}
/**
* Removes instance from registry by name or instance
*

View File

@@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog;
use Monolog\Logger;
use Monolog\Registry;
class RegistryTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
Registry::clear();
}
/**
* @dataProvider hasLoggerProvider
* @covers Monolog\Registry::hasLogger
*/
public function testHasLogger(array $loggersToAdd, array $loggersToCheck, array $expectedResult)
{
foreach ($loggersToAdd as $loggerToAdd) {
Registry::addLogger($loggerToAdd);
}
foreach ($loggersToCheck as $index => $loggerToCheck) {
$this->assertSame($expectedResult[$index], Registry::hasLogger($loggerToCheck));
}
}
public function hasLoggerProvider()
{
$logger1 = new Logger('test1');
$logger2 = new Logger('test2');
$logger3 = new Logger('test3');
return array(
// only instances
array(
array($logger1),
array($logger1, $logger2),
array(true, false),
),
// only names
array(
array($logger1),
array('test1', 'test2'),
array(true, false),
),
// mixed case
array(
array($logger1, $logger2),
array('test1', $logger2, 'test3', $logger3),
array(true, true, false, false),
),
);
}
}