Skip to content
Permalink
Browse files

Use psr LoggerInterface where possible

  • Loading branch information...
mabar authored and f3l1x committed Apr 29, 2019
1 parent 4ab1b55 commit d621f62a11f1de89766f78c1ae6b4b64f6a0f7ba
@@ -45,19 +45,17 @@ monolog:
Log message with injected logger (only `default` is autowired)

```php
use Monolog\Logger;
use Psr\Log\LoggerInterface;
class ExampleService
{
/** @var Logger **/
/** @var LoggerInterface **/
private $logger;
public function injectLogger(Logger $logger): void
public function injectLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
// or withName if you want change channel name
$this->logger = $logger->withName('example');
}
public function doSomething(): void
@@ -86,7 +84,7 @@ class ExampleService
{
/** @var ILoggerManager **/
private $logger;
private $loggerManager;
public function injectLoggerManager(ILoggerManager $loggerManager): void
{
@@ -101,8 +101,7 @@ public function loadConfiguration(): void
}
$logger = $builder->addDefinition($this->prefix('logger.' . $name))
->setType(Logger::class)
->setArguments([
->setFactory(Logger::class, [
$name,
$channel['handlers'],
$channel['processors'] ?? [],
@@ -2,13 +2,13 @@
namespace Contributte\Monolog;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
interface ILoggerManager
{
public function has(string $name): bool;
public function get(string $name): Logger;
public function get(string $name): LoggerInterface;
}
@@ -3,8 +3,8 @@
namespace Contributte\Monolog;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Monolog\Logger;
use Nette\DI\Container;
use Psr\Log\LoggerInterface;
class LazyLoggerManager implements ILoggerManager
{
@@ -26,7 +26,7 @@ public function has(string $name): bool
return $this->container->hasService(sprintf('%s.%s', $this->prefix, $name));
}
public function get(string $name): Logger
public function get(string $name): LoggerInterface
{
if (!$this->has($name)) {
throw new InvalidStateException(sprintf('Cannot get undefined logger "%s".', $name));
@@ -4,6 +4,7 @@
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
class LoggerHolder
{
@@ -22,6 +23,9 @@ public function __construct(Logger $logger)
$this->instLogger = $logger;
}
/**
* @return static
*/
public static function getInstance(): self
{
if (static::$logger === null) {
@@ -40,7 +44,7 @@ public static function setLogger(Logger $logger): void
static::$logger = $logger;
}
public function getLogger(): Logger
public function getLogger(): LoggerInterface
{
$backtrace = debug_backtrace();
// Get class which called this or file if class does not exist
@@ -4,6 +4,7 @@
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
class LoggerManager implements ILoggerManager
{
@@ -27,7 +28,7 @@ public function add(Logger $logger): void
$this->loggers[$name] = $logger;
}
public function get(string $name): Logger
public function get(string $name): LoggerInterface
{
if (!$this->has($name)) {
throw new InvalidStateException(sprintf('Cannot get undefined logger "%s".', $name));
@@ -9,6 +9,7 @@
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Tests\Contributte\Monolog\NeonLoader;
class MonologExtensionTest extends TestCase
@@ -28,14 +29,16 @@ public function testRegistration(): void
'));
/** @var Logger $default */
$default = $container->getByType(Logger::class);
$default = $container->getByType(LoggerInterface::class);
$this->assertInstanceOf(Logger::class, $default);
$this->assertEquals('default', $default->getName());
/** @var Logger $foo */
$foo = $container->getService('monolog.logger.foo');
$this->assertInstanceOf(Logger::class, $foo);
$this->assertInstanceOf(LoggerInterface::class, $foo);
$this->assertEquals('foo', $foo->getName());
$this->assertInstanceOf(Logger::class, $container->getByType(Logger::class));
}
public function testRegistrationNoDefault(): void
@@ -44,8 +47,6 @@ public function testRegistrationNoDefault(): void
$this->expectExceptionMessage('monolog.channel.default is required.');
$container = $this->createContainer([]);
$container->getByType(Logger::class);
}
/**

0 comments on commit d621f62

Please sign in to comment.
You can’t perform that action at this time.