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

#70 - implemented new AbstractFactory for PsrLoggerAdapter #99

Merged
merged 7 commits into from
Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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).
69 changes: 69 additions & 0 deletions docs/book/service-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,75 @@ 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,
],
],
];
```

Additionally, instead of using the `log` configuration key, you will use the key
`psr_log`:

```php
// module.config.php

use Zend\Log\Formatter\Simple;
use Zend\Log\Logger;
use Zend\Log\Processor\RequestId;

return [
'psr_log' => [ // <-- NOTE: key change!
'MyLogger' => [
'writers' => [
'stream' => [
'name' => 'stream',
'priority' => 1,
'options' => [
'stream' => 'php://output',
'formatter' => [
'name' => Simple::class,
'options' => [
'format' => '%timestamp% %priorityName% (%priority%): %message% %extra%',
'dateTimeFormat' => 'c',
],
],
'filters' => [
'priority' => [
'name' => 'priority',
'options' => [
'operator' => '<=',
'priority' => Logger::INFO,
],
],
],
],
],
],
'processors' => [
'requestid' => [
'name' => RequestId::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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}