Skip to content
Permalink
Browse files

Drop non-lazy LoggerManager and ILoggerManager interface

  • Loading branch information...
mabar authored and f3l1x committed May 6, 2019
1 parent 2ae8e6c commit 9eae477f8489bf249681d76d2f4d74bdffbca1c5
@@ -75,27 +75,26 @@ You could also use logger manager in case you need to use multiple logger at onc
monolog:
manager:
enabled: false # disabled by default
lazy: true # lazy by default
```

```php
use Contributte\Monolog\ILoggerManager;
use Contributte\Monolog\LoggerManager;
class ExampleService
{
/** @var ILoggerManager **/
/** @var LoggerManager **/
private $loggerManager;
public function injectLoggerManager(ILoggerManager $loggerManager): void
public function injectLoggerManager(LoggerManager $loggerManager): void
{
$this->loggerManager = $loggerManager;
}
public function doSomething(): void
{
$this->loggerManager->get('default')->info('Log that application did something');
$this->loggerManager->get('specialLogger')->info('Log something very special')
$this->loggerManager->get('specialLogger')->info('Log something very special');
}
}
@@ -6,6 +6,5 @@ includes:

parameters:
ignoreErrors:
- '#^Variable \$manager might not be defined\.$#'
- '#^Call to deprecated method formatPhp\(\) of class Nette\\DI\\ContainerBuilder\.$#'
- '#^Method Contributte\\Monolog\\Tracy\\LazyTracyLogger\:\:log\(\) has parameter (\$priority|\$value) with no typehint specified\.$#'
@@ -4,7 +4,6 @@
use Contributte\Monolog\Exception\Logic\InvalidArgumentException;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LazyLoggerManager;
use Contributte\Monolog\LoggerManager;
use Contributte\Monolog\Tracy\LazyTracyLogger;
use Monolog\Handler\PsrHandler;
@@ -34,7 +33,6 @@ class MonologExtension extends CompilerExtension
],
'manager' => [
'enabled' => false,
'lazy' => true,
],
];
@@ -54,13 +52,10 @@ public function loadConfiguration(): void
}
if ($config['manager']['enabled']) {
$manager = $builder->addDefinition($this->prefix('manager'));
if ($config['manager']['lazy']) {
$manager->setFactory(LazyLoggerManager::class, ['prefix' => $this->prefix('logger')]);
} else {
$manager->setFactory(LoggerManager::class);
}
$builder->addDefinition($this->prefix('manager'))
->setFactory(LoggerManager::class, [
$this->prefix('logger'),
]);
}
$tracyHandler = null;
@@ -127,10 +122,6 @@ public function loadConfiguration(): void
$channel['processors'] ?? [],
]);
if ($config['manager']['enabled'] === true && $config['manager']['lazy'] !== true) {
$manager->addSetup('add', [$logger]);
}
// Only default logger is autowired
if ($name !== 'default') {
$logger->setAutowired(false);

This file was deleted.

This file was deleted.

@@ -3,29 +3,27 @@
namespace Contributte\Monolog;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Monolog\Logger;
use Nette\DI\Container;
use Psr\Log\LoggerInterface;
class LoggerManager implements ILoggerManager
class LoggerManager
{
/** @var Logger[] */
private $loggers;
/** @var string */
private $prefix;
public function has(string $name): bool
/** @var Container */
private $container;
public function __construct(string $prefix, Container $container)
{
return isset($this->loggers[$name]);
$this->prefix = $prefix;
$this->container = $container;
}
public function add(Logger $logger): void
public function has(string $name): bool
{
$name = $logger->getName();
if ($this->has($name)) {
throw new InvalidStateException(sprintf('Cannot add logger with name "%s". Logger with same name is already defined.', $name));
}
$this->loggers[$name] = $logger;
return $this->container->hasService(sprintf('%s.%s', $this->prefix, $name));
}
public function get(string $name): LoggerInterface
@@ -34,7 +32,7 @@ public function get(string $name): LoggerInterface
throw new InvalidStateException(sprintf('Cannot get undefined logger "%s".', $name));
}
return $this->loggers[$name];
return $this->container->getService(sprintf('%s.%s', $this->prefix, $name));
}
}

This file was deleted.

@@ -4,6 +4,7 @@
use Contributte\Monolog\DI\MonologExtension;
use Contributte\Monolog\Exception\Logic\InvalidStateException;
use Contributte\Monolog\LoggerManager;
use Monolog\Logger;
use Nette\DI\Compiler;
use Nette\DI\Container;
@@ -26,6 +27,8 @@ public function testRegistration(): void
foo:
handlers:
- Monolog\Handler\NullHandler
manager:
enabled: true
'));
/** @var Logger $default */
@@ -39,6 +42,15 @@ public function testRegistration(): void
$this->assertEquals('foo', $foo->getName());
$this->assertInstanceOf(Logger::class, $container->getByType(Logger::class));
/** @var LoggerManager $manager */
$manager = $container->getByType(LoggerManager::class);
$this->assertTrue($manager->has('default'));
$this->assertSame($default, $manager->get('default'));
$this->assertTrue($manager->has('foo'));
$this->assertSame($foo, $manager->get('foo'));
}
public function testRegistrationNoDefault(): void

0 comments on commit 9eae477

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