Issues

ZF-9790: allow to attach a formatter from .ini configuration and do not hardcode formatter class in Log_Writer class

Issue Type: Improvement Created: 2010-05-02T14:07:06.000+0000 Last Updated: 2011-09-19T13:24:33.000+0000 Status: Resolved Fix version(s): - 1.11.3 (01/Feb/11)

Reporter: Remy Damour (remy215) Assignee: Benoît Durand (intiilapa) Tags: - Zend_Log

Related issues: - ZF-9176

Attachments: - Log_config-formatter.patch

Description

It would be great if we could attach a formatter directly through application.ini file AND without having to subclass a writer either.

Currently we can define the following entries for Zend_Log:

<pre class="highlight">
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../logs/app.log"
resources.log.stream.writerNamespace = "Library_Log_Writer"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = Zend_Log::WARN

// It would be great if we could add something like:
resources.log.stream.formatterName = "MyFormaterName"
resources.log.stream.formatterNamespace = "My_Formater_Namespace"

This would improve logging customization a lot and make it easier. It would be absolutely necessary to remove currently hardcoded formatter classes within Writer classes.

<pre class="highlight">
        $this->_formatter = new Zend_Log_Formatter_Simple();

=> currently, to attach your own formatter, you have to create your own subclass of Writter_Stream... Not very flexible...

Comments

Posted by Erick Wilder de Oliveira (erickwilder) on 2010-06-02T05:45:54.000+0000

Not sure if is really necessary to extend a Writer. Just setting a Formatter to the current Writer doesn't does the trick?

Posted by Aaron S. Hawley (ashawley) on 2010-06-18T14:26:31.000+0000

Here's a patch against 1.10.4 that simply duplicates the way writers and filters are generated as application resources.

I tested only:

<pre class="literal">
resources.log.db.formatterName = "Simple"
resources.log.db.formatterParams.format = '%timestamp%: %message%'

Not the testing of Namespace.

Posted by Benoît Durand (intiilapa) on 2010-12-30T09:54:02.000+0000

r23599

Posted by Aaron S. Hawley (ashawley) on 2011-01-03T08:53:58.000+0000

Benoit, thanks for taking this on.

Perhaps, this should be added to the existing tests for Zend_Application_Resource_Log?

<pre class="literal">
Index: trunk/tests/Zend/Application/Resource/LogTest.php
===================================================================
--- trunk/tests/Zend/Application/Resource/LogTest.php   (revision 23599)
+++ trunk/tests/Zend/Application/Resource/LogTest.php   (working copy)
@@ -109,5 +109,13 @@
             'writerParams' => array(
                 'stream' => $stream,
-            )
+            ),
+            'filterName'   => 'Priority',
+            'filterParams' => array(
+                'priority' => Zend_Log::INFO,
+            ),
+            'formatterName'   => 'Simple',
+            'formatterParams' => array(
+                'format' => '%timestamp%: %message%',
+            ),
         ));
 
@@ -121,5 +129,7 @@
         $log->log($message = 'logged-message', Zend_Log::INFO);
         rewind($stream);
-        $this->assertContains($message, stream_get_contents($stream));
+        $contents = stream_get_contents($stream);
+        $this->assertStringEndsWith($message, $contents);
+        $this->assertRegexp('/\d\d:\d\d:\d\d/', $contents);
     }

Posted by Matthew Weier O'Phinney (matthew) on 2011-01-04T06:04:18.000+0000

Benoit -- patch looks good, but I also think Aaron's point is valid. Once the Log resource test is added, feel free to merge to the 1.11 release branch.

Posted by Benoît Durand (intiilapa) on 2011-01-23T01:16:18.000+0000

I have committed the unit test of Aaron in the trunk r23642. And, I added documentation in r23675.

Posted by Alberto (neofraktal) on 2011-02-10T00:48:12.000+0000

Documentation appears as:

filterParams -> format

but you must to use:

formatterParams -> format

Posted by Peter Culak (fr0z3nk0) on 2011-09-14T12:56:08.000+0000

I'm using tag http://framework.zend.com/svn/framework/…. this setting in application.ini just gets ignored: resources.log.stream.formatterNamespace = "Model_Log_Formatter"

the reason for it is that Zend_Log calls this on line 225: $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);

has this fix not been included in release yet ?

peter

Posted by Benoît Durand (intiilapa) on 2011-09-14T18:31:52.000+0000

What fix are you talking about?

Posted by Peter Culak (fr0z3nk0) on 2011-09-15T08:56:25.000+0000

fix which would allow to set a namespace from through config file. the patch attached above doesn't allow that as it does: protected function _constructFormatterFromConfig($config) + { + $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);

$this->_defaultFormatterNamespace should probably pick up formatterNamespace from config first and if not set then use default one.

Posted by Benoît Durand (intiilapa) on 2011-09-15T17:43:38.000+0000

This feature is added since version 1.11.3. Would you have a sample of a configuration that does not work?

You make a misreading of the source code. You should read the getClassName method: a default namespace is used only if the configuration does not provide to complete the short name.

<pre class="highlight">
        // line 295
        $namespace = $defaultNamespace;
        if (isset($config[ $type . 'Namespace' ])) {
            $namespace = $config[ $type . 'Namespace' ];
        }

        $fullClassName = $namespace . '_' . $className;

You should open a new issue as bug to discuss if there really is a bug on this feature.

Posted by Peter Culak (fr0z3nk0) on 2011-09-19T13:24:33.000+0000

you're right, it works fine. I was extending formatter and i didn't overload factory method so it was returning wrong class.

thanx for your time ;)

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts