Skip to content

Commit

Permalink
Drop non-lazy LoggerManager and ILoggerManager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar authored and Milan Felix Šulc committed May 29, 2019
1 parent 2ae8e6c commit 9eae477
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 135 deletions.
9 changes: 4 additions & 5 deletions .docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Expand Down
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\.$#'
17 changes: 4 additions & 13 deletions src/DI/MonologExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,7 +33,6 @@ class MonologExtension extends CompilerExtension
],
'manager' => [
'enabled' => false,
'lazy' => true,
],
];

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 0 additions & 14 deletions src/ILoggerManager.php

This file was deleted.

38 changes: 0 additions & 38 deletions src/LazyLoggerManager.php

This file was deleted.

28 changes: 13 additions & 15 deletions src/LoggerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

}
49 changes: 0 additions & 49 deletions tests/LoggerManagerTest.php

This file was deleted.

12 changes: 12 additions & 0 deletions tests/Unit/DI/MonologExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,6 +27,8 @@ public function testRegistration(): void
foo:
handlers:
- Monolog\Handler\NullHandler
manager:
enabled: true
'));

/** @var Logger $default */
Expand All @@ -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
Expand Down

0 comments on commit 9eae477

Please sign in to comment.