Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge 5ad8aac into fda6e00
Browse files Browse the repository at this point in the history
  • Loading branch information
dennybrandes committed Dec 27, 2019
2 parents fda6e00 + 5ad8aac commit e6d71b1
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#99](https://github.com/zendframework/zend-log/pull/99) adds `Zend\Log\PsrLoggerAbstractAdapterFactory`, which will create instances of `PsrLoggerAdapter`. Usage is exactly like with `Zend\Log\LoggerAbstractServiceFactory`, with the exception that it looks under the `psr_log` configuration key instead of the `log` key for logger configuration.

### Changed

Expand Down
5 changes: 5 additions & 0 deletions docs/book/psr3.md
Expand Up @@ -61,3 +61,8 @@ $logger->addProcessor(new Zend\Log\Processor\PsrPlaceholder);
$logger->info('User with email {email} registered', ['email' => 'user@example.org']);
// logs message 'User with email user@example.org registered'
```

## Usage with zend-servicemanager

For usage with zend-servicemanager, read the [`PsrLoggerAbstractServiceFactory`
documentation](service-manager.md#psrloggerabstractservicefactory).
22 changes: 22 additions & 0 deletions docs/book/service-manager.md
Expand Up @@ -110,6 +110,28 @@ Because the main filter is `Priority`, it can be set directly too:
];
```

## PsrLoggerAbstractServiceFactory

As with the [`LoggerAbstractServiceFactory` above](#loggerabstractservicefactory),
you can use `PsrLoggerAbstractServiceFactory` to create [PSR-3-conforming
logger instances](psr3.md). Register it as an abstract factory in your
configuration; as an example:

```php
// module.config.php

use Zend\Log\PsrLoggerAbstractServiceFactory;

return [
'service_manager' => [
'abstract_factories' => [
PsrLoggerAbstractServiceFactory::class,
],
],
];
```


## Custom Writers, Formatters, Filters, and Processors

In the `LoggerAbstractServiceFactory` example above, a custom formatter (called
Expand Down
1 change: 1 addition & 0 deletions src/ConfigProvider.php
Expand Up @@ -33,6 +33,7 @@ public function getDependencyConfig()
return [
'abstract_factories' => [
LoggerAbstractServiceFactory::class,
PsrLoggerAbstractAdapterFactory::class,
],
'factories' => [
Logger::class => LoggerServiceFactory::class,
Expand Down
34 changes: 34 additions & 0 deletions src/PsrLoggerAbstractAdapterFactory.php
@@ -0,0 +1,34 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-log for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Log;

use Interop\Container\ContainerInterface;

/**
* PSR Logger abstract service factory.
*
* Allow to configure multiple loggers for application.
*/
class PsrLoggerAbstractAdapterFactory extends LoggerAbstractServiceFactory
{
/**
* Configuration key holding logger configuration
*
* @var string
*/
protected $configKey = 'psr_log';

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$logger = parent::__invoke($container, $requestedName, $options);

return new PsrLoggerAdapter($logger);
}
}
90 changes: 90 additions & 0 deletions test/PsrLoggerAbstractAdapterFactoryTest.php
@@ -0,0 +1,90 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zend-log for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Log;

use PHPUnit\Framework\TestCase;
use Zend\Log\LoggerAbstractServiceFactory;
use Zend\Log\PsrLoggerAbstractAdapterFactory;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceManager;

class PsrLoggerAbstractAdapterFactoryTest extends TestCase
{
/**
* @var \Zend\ServiceManager\ServiceLocatorInterface
*/
protected $serviceManager;

/**
* Set up LoggerAbstractServiceFactory and loggers configuration.
*/
protected function setUp()
{
$this->serviceManager = new ServiceManager();
$config = new Config([
'abstract_factories' => [PsrLoggerAbstractAdapterFactory::class],
'services' => [
'config' => [
'psr_log' => [
'Application\Frontend' => [],
'Application\Backend' => [],
],
],
],
]);
$config->configureServiceManager($this->serviceManager);
}

/**
* @return array
*/
public function providerValidLoggerService()
{
return [
['Application\Frontend'],
['Application\Backend'],
];
}

/**
* @return array
*/
public function providerInvalidLoggerService()
{
return [
['Logger\Application\Unknown'],
['Logger\Application\Frontend'],
['Application\Backend\Logger'],
];
}

/**
* @param string $service
* @dataProvider providerValidLoggerService
*/
public function testValidLoggerService($service)
{
$actual = $this->serviceManager->get($service);
$this->assertInstanceOf('Zend\Log\PsrLoggerAdapter', $actual);
}

/**
* @dataProvider providerInvalidLoggerService
*
* @param string $service
*/
public function testInvalidLoggerService($service)
{
$this->expectException(ServiceNotFoundException::class);
$this->serviceManager->get($service);
}
}

0 comments on commit e6d71b1

Please sign in to comment.