Added abstract service factory for logger component to provide several loggers for application. #3930

Merged
merged 5 commits into from Mar 11, 2013

Projects

None yet

5 participants

@stephanoff
Contributor

Added support for multiple loggers in application.
Configuration should be like this:

'logger' => array(
    'Backend\Logger' => array(
        'writers' => array(
            array(
                'name' => 'stream',
                'priority' => 1,
                'options' => array(
                    'stream' => __DIR__ . '/data/log/backend.log',
                ),
                'filters' => array(
                    array(
                        'name' => 'priority',
                        'options' => array(
                            'priority' => E_STRICT | E_WARNING | E_NOTICE
                        ),
                    ),
                ),
                'formatter' => array(
                    'name' => 'base',
                    'options' => 'Y-m-d H:i:s',
                ),
            ),
        ),
    ),
    'Frontend\Logger' => array(
        'writers' => array(
            array(
                'name' => 'stream',
                'priority' => 1,
                'options' => array(
                    'stream' => __DIR__ . '/data/log/frontend.log',
                ),
            ),
        ),
    ),
)
weierophinney and others added some commits Mar 1, 2013
@weierophinney weierophinney Merge branch 'hotfix/3767'
Close #3767
Fixes #3696
Fixes #2752
e09ba97
@stephanoff stephanoff Added abstract service factory to provide multiple loggers
configuration.

Change-Id: Ibd9309bf42c8992376849a1c5bbae05cd82e4c13
a99b8c0
@blanchonvincent blanchonvincent commented on an outdated diff Mar 2, 2013
library/Zend/Log/LoggerAbstractServiceFactory.php
+ * @param string $name
+ * @param string $requestedName
+ * @return bool
+ */
+ public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
+ {
+ $config = $serviceLocator->get('Config');
+
+ if (isset($config['log'][$name])) {
+ return true;
+
+ } else if (isset($config['log'][$requestedName])) {
+ return true;
+
+ } else {
+ return false;
@blanchonvincent
blanchonvincent Mar 2, 2013 Contributor

delete the "else", finish with "return false" without else

@blanchonvincent blanchonvincent commented on an outdated diff Mar 2, 2013
library/Zend/Log/LoggerAbstractServiceFactory.php
+ * @param string $name
+ * @param string $requestedName
+ * @return \Zend\Log\Logger
+ */
+ public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
+ {
+ $config = $serviceLocator->get('Config');
+
+ if (isset($config['log'][$name])) {
+ return new Logger($config['log'][$name]);
+
+ } else if (isset($config['log'][$requestedName])) {
+ return new Logger($config['log'][$requestedName]);
+
+ } else {
+ return new Logger();
@blanchonvincent
blanchonvincent Mar 2, 2013 Contributor

It's not a normal use case ? you should throw exception or delete the second condition because you have checked the config

@stephanoff stephanoff Remove redundant "else" statement and redundant condition in factory
method.

Change-Id: Idf4802f8735a2333d927f5c6989e124c048e7854
56972fd
@stephanoff
Contributor
@blanchonvincent
Contributor

@stephanoff ok, now you must add unit tests with the PR

@stephanoff stephanoff Added test case for logger abstract service factory.
Change-Id: Ibd0e492df076b801cb91078753bfe40bf77f63c0
79ed445
@stephanoff
Contributor

@blanchonvincent Test case added.

@weierophinney weierophinney was assigned Mar 8, 2013
@stephanoff stephanoff Minor improvement in validation of configuration.
Change-Id: I8e342727e72db510a45b885ddb293ceef4590510
49afaf4
@weierophinney
Member

@stephanoff This is brilliant. I'd like to see this approach adopted in more places, to be honest!

@weierophinney weierophinney added a commit that referenced this pull request Mar 11, 2013
@weierophinney weierophinney Merge branch 'feature/3930' into develop
Close #3930
e8e1398
@weierophinney weierophinney merged commit 49afaf4 into zendframework:develop Mar 11, 2013

1 check passed

default The Travis build passed
Details
@stephanoff stephanoff deleted the unknown repository branch Mar 12, 2013
@localheinz localheinz commented on the diff May 18, 2013
tests/ZendTest/Log/LoggerAbstractServiceFactoryTest.php
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_Log
+ */
+
+namespace ZendTest\Log;
+
+use Zend\ServiceManager\ServiceManager;
+use Zend\Mvc\Service\ServiceManagerConfig;
+
+/**
+ * @category Zend
+ * @package Zend_Log
+ * @subpackage UnitTests
+ * @group Zend_Log
+ */
+class LoggerAbstractServiceFactoryTeset extends \PHPUnit_Framework_TestCase
@steptom
steptom commented May 25, 2013

I have recently migrate to the 2.2.0 version of Zend Framework and I can't add a filters and formatter to the writer by using the configuration you give as example.
Is it normal ?

(It would be unfortunate to be able to create loggers without being able to specify filters and formatter)

@stephanoff
Contributor

Hi @steptom,

Try to use 'log' section name instead of 'logger'.
It changes due to compatibility with LoggerServiceFactory.

@steptom
steptom commented May 26, 2013

Hi @stephanoff ,
thanks for your answer, this allowed me to partially solve my problem.

In fact, the example has a lot of errors ;)
(For example the formatter and filters parameters are writer options)

@weierophinney weierophinney added a commit to zendframework/zend-log that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge pull request zendframework/zendframework#3930 from stephanoff/d…
…evelop-logger

Added abstract service factory for logger component to provide several loggers for application.
5e733b5
@weierophinney weierophinney added a commit to zendframework/zend-log that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/3930' into develop c1290fa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment