From a3e68927ea8b48a3bb8dc580ac28092a5a6642a0 Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Sun, 25 Aug 2019 09:28:59 +0200 Subject: [PATCH 1/7] - implemented new AbstractFactory for PsrLoggerAdapter --- src/ConfigProvider.php | 1 + src/PsrLoggerAbstractAdapterFactory.php | 34 ++++++++++ test/PsrLoggerAbstractAdapterFactoryTest.php | 67 ++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/PsrLoggerAbstractAdapterFactory.php create mode 100644 test/PsrLoggerAbstractAdapterFactoryTest.php diff --git a/src/ConfigProvider.php b/src/ConfigProvider.php index 98df45e1..399b8185 100644 --- a/src/ConfigProvider.php +++ b/src/ConfigProvider.php @@ -33,6 +33,7 @@ public function getDependencyConfig() return [ 'abstract_factories' => [ LoggerAbstractServiceFactory::class, + PsrLoggerAbstractAdapterFactory::class, ], 'factories' => [ Logger::class => LoggerServiceFactory::class, diff --git a/src/PsrLoggerAbstractAdapterFactory.php b/src/PsrLoggerAbstractAdapterFactory.php new file mode 100644 index 00000000..e9c22ad2 --- /dev/null +++ b/src/PsrLoggerAbstractAdapterFactory.php @@ -0,0 +1,34 @@ +serviceManager = new ServiceManager(); + $config = new Config([ + 'abstract_factories' => [PsrLoggerAbstractAdapterFactory::class], + 'services' => [ + 'config' => [ + 'psr_log' => [ + 'Application\Frontend' => [], + 'Application\Backend' => [], + ], + ], + ], + ]); + $config->configureServiceManager($this->serviceManager); + } + + /** + * @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); + } +} From 9aeaed4178be752eda1b1499dbb847184f09ce04 Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Sun, 25 Aug 2019 09:39:39 +0200 Subject: [PATCH 2/7] add missing test providers --- test/PsrLoggerAbstractAdapterFactoryTest.php | 25 +++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test/PsrLoggerAbstractAdapterFactoryTest.php b/test/PsrLoggerAbstractAdapterFactoryTest.php index fa98e7d0..35526612 100644 --- a/test/PsrLoggerAbstractAdapterFactoryTest.php +++ b/test/PsrLoggerAbstractAdapterFactoryTest.php @@ -32,7 +32,7 @@ protected function setUp() $this->serviceManager = new ServiceManager(); $config = new Config([ 'abstract_factories' => [PsrLoggerAbstractAdapterFactory::class], - 'services' => [ + 'services' => [ 'config' => [ 'psr_log' => [ 'Application\Frontend' => [], @@ -44,6 +44,29 @@ protected function setUp() $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 From 23411e89e9bfc520748b503dee49dbe7b822b78d Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Sun, 25 Aug 2019 09:46:21 +0200 Subject: [PATCH 3/7] fixed cs issues --- src/PsrLoggerAbstractAdapterFactory.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PsrLoggerAbstractAdapterFactory.php b/src/PsrLoggerAbstractAdapterFactory.php index e9c22ad2..0d86b2d8 100644 --- a/src/PsrLoggerAbstractAdapterFactory.php +++ b/src/PsrLoggerAbstractAdapterFactory.php @@ -27,8 +27,8 @@ class PsrLoggerAbstractAdapterFactory extends LoggerAbstractServiceFactory public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { - $logger = parent::__invoke($container, $requestedName,$options); + $logger = parent::__invoke($container, $requestedName, $options); return new PsrLoggerAdapter($logger); } -} \ No newline at end of file +} From de56c0c6f23bad4b8c44b016bc0bc21b29bcf8cf Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Sun, 25 Aug 2019 10:01:05 +0200 Subject: [PATCH 4/7] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d078d4ce..3c1f7b43 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) Added AbstractFactory for PsrLoggerAdapter. ### Changed From 17f2cf2b0339ae116f14aa96d15f6b56f20221df Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Sun, 25 Aug 2019 10:01:05 +0200 Subject: [PATCH 5/7] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c1f7b43..db6ae44b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file, in reverse ### Added -- [#99](https://github.com/zendframework/zend-log/pull/99) Added AbstractFactory for PsrLoggerAdapter. +- [#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 From a89f70357228700446ff635e1e1531f0d46afd0e Mon Sep 17 00:00:00 2001 From: Denny Brandes Date: Wed, 4 Dec 2019 16:24:20 +0100 Subject: [PATCH 6/7] added documentation for PsrLoggerAbstractServiceFactory --- docs/book/psr3.md | 3 +++ docs/book/service-manager.md | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/docs/book/psr3.md b/docs/book/psr3.md index d5c3b7d6..0aa0a98a 100644 --- a/docs/book/psr3.md +++ b/docs/book/psr3.md @@ -61,3 +61,6 @@ $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 ServiceManager + +For usage with ServiceManager, check this [PsrLoggerAbstractServiceFactory](./service-manager.md#PsrLoggerAbstractServiceFactory). \ No newline at end of file diff --git a/docs/book/service-manager.md b/docs/book/service-manager.md index 6ec1cc7e..e54795d2 100644 --- a/docs/book/service-manager.md +++ b/docs/book/service-manager.md @@ -110,6 +110,23 @@ Because the main filter is `Priority`, it can be set directly too: ]; ``` +## PsrLoggerAbstractServiceFactory + +Same as above, you can use PsrLoggerAbstractServiceFactory to create a PSR-3 conform logger. +Just use following configuration instead. + +```php +// module.config.php +return [ + 'service_manager' => [ + 'abstract_factories' => [ + 'Zend\Log\PsrLoggerAbstractServiceFactory', + ], + ], +]; +``` + + ## Custom Writers, Formatters, Filters, and Processors In the `LoggerAbstractServiceFactory` example above, a custom formatter (called From b2f3f8e3c17a064cde389508f2324b8d51bb1773 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Fri, 27 Dec 2019 09:37:14 -0600 Subject: [PATCH 7/7] docs: incorporate feedback This patch incorporates feedback from @froschdesign. --- docs/book/psr3.md | 6 ++-- docs/book/service-manager.md | 58 ++++++++++++++++++++++++++++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/docs/book/psr3.md b/docs/book/psr3.md index 0aa0a98a..80529e61 100644 --- a/docs/book/psr3.md +++ b/docs/book/psr3.md @@ -61,6 +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 ServiceManager -For usage with ServiceManager, check this [PsrLoggerAbstractServiceFactory](./service-manager.md#PsrLoggerAbstractServiceFactory). \ No newline at end of file +## Usage with zend-servicemanager + +For usage with zend-servicemanager, read the [`PsrLoggerAbstractServiceFactory` +documentation](service-manager.md#psrloggerabstractservicefactory). diff --git a/docs/book/service-manager.md b/docs/book/service-manager.md index e54795d2..a78d7d7d 100644 --- a/docs/book/service-manager.md +++ b/docs/book/service-manager.md @@ -112,20 +112,72 @@ Because the main filter is `Priority`, it can be set directly too: ## PsrLoggerAbstractServiceFactory -Same as above, you can use PsrLoggerAbstractServiceFactory to create a PSR-3 conform logger. -Just use following configuration instead. +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' => [ - 'Zend\Log\PsrLoggerAbstractServiceFactory', + 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