Browse Source

ZF-9176 and ZF-9790: implement Zend_Log_Formatter::factory()

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23599 44c647ce-9c0f-0410-b52a-842ac1e357ba
intiilapa 15 years ago
parent
commit
e9a1411e62

+ 35 - 1
library/Zend/Log.php

@@ -72,6 +72,12 @@ class Zend_Log
 
     /**
      *
+     * @var string
+     */
+    protected $_defaultFormatterNamespace = 'Zend_Log_Formatter';
+
+    /**
+     *
      * @var callback
      */
     protected $_origErrorHandler       = null;
@@ -169,6 +175,11 @@ class Zend_Log
             $writer->addFilter($filter);
         }
 
+        if (isset($config['formatterName'])) {
+            $formatter = $this->_constructFormatterFromConfig($config);
+            $writer->setFormatter($formatter);
+        }
+
         return $writer;
     }
 
@@ -195,6 +206,29 @@ class Zend_Log
         return $filter;
     }
 
+   /**
+     * Construct formatter object from configuration array or Zend_Config object
+     *
+     * @param  array|Zend_Config $config Zend_Config or Array
+     * @return Zend_Log_Formatter_Interface
+     * @throws Zend_Log_Exception
+     */
+    protected function _constructFormatterFromConfig($config)
+    {
+        $formatter = $this->_constructFromConfig('formatter', $config, $this->_defaultFormatterNamespace);
+
+        if (!$formatter instanceof Zend_Log_Formatter_Interface) {
+             $formatterName = is_object($formatter)
+                         ? get_class($formatter)
+                         : 'The specified formatter';
+            /** @see Zend_Log_Exception */
+            require_once 'Zend/Log/Exception.php';
+            throw new Zend_Log_Exception($formatterName . ' does not implement Zend_Log_Formatter_Interface');
+        }
+
+        return $formatter;
+    }
+
     /**
      * Construct a filter or writer from config
      *
@@ -228,7 +262,7 @@ class Zend_Log
         if (!$reflection->implementsInterface('Zend_Log_FactoryInterface')) {
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception(
-                'Driver does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
+                $className . ' does not implement Zend_Log_FactoryInterface and can not be constructed from config.'
             );
         }
 

+ 40 - 0
library/Zend/Log/Formatter/Abstract.php

@@ -0,0 +1,40 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Log
+ * @subpackage Formatter
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+/** @see Zend_Log_Formatter_Interface */
+require_once 'Zend/Log/Formatter/Interface.php';
+
+/** @see Zend_Log_FactoryInterface */
+require_once 'Zend/Log/FactoryInterface.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Log
+ * @subpackage Formatter
+ * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+abstract class Zend_Log_Formatter_Abstract
+    implements Zend_Log_Formatter_Interface, Zend_Log_FactoryInterface
+{
+}

+ 15 - 4
library/Zend/Log/Formatter/Firebug.php

@@ -20,8 +20,8 @@
  * @version    $Id$
  */
 
-/** Zend_Log_Formatter_Interface */
-require_once 'Zend/Log/Formatter/Interface.php';
+/** Zend_Log_Formatter_Abstract */
+require_once 'Zend/Log/Formatter/Abstract.php';
 
 /**
  * @category   Zend
@@ -30,9 +30,20 @@ require_once 'Zend/Log/Formatter/Interface.php';
  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  */
-class Zend_Log_Formatter_Firebug implements Zend_Log_Formatter_Interface
+class Zend_Log_Formatter_Firebug extends Zend_Log_Formatter_Abstract
 {
     /**
+	 * Factory for Zend_Log_Formatter_Firebug classe
+	 *
+     * @param array|Zend_Config $options useless
+	 * @return Zend_Log_Formatter_Firebug
+     */
+    public static function factory($options)
+    {
+        return new self;
+    }
+
+    /**
      * This method formats the event for the firebug writer.
      *
      * The default is to just send the message parameter, but through
@@ -47,4 +58,4 @@ class Zend_Log_Formatter_Firebug implements Zend_Log_Formatter_Interface
     {
         return $event['message'];
     }
-}
+}

+ 31 - 9
library/Zend/Log/Formatter/Simple.php

@@ -20,8 +20,8 @@
  * @version    $Id$
  */
 
-/** Zend_Log_Formatter_Interface */
-require_once 'Zend/Log/Formatter/Interface.php';
+/** Zend_Log_Formatter_Abstract */
+require_once 'Zend/Log/Formatter/Abstract.php';
 
 /**
  * @category   Zend
@@ -31,7 +31,7 @@ require_once 'Zend/Log/Formatter/Interface.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version    $Id$
  */
-class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
+class Zend_Log_Formatter_Simple extends Zend_Log_Formatter_Abstract
 {
     /**
      * @var string
@@ -53,7 +53,7 @@ class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
             $format = self::DEFAULT_FORMAT . PHP_EOL;
         }
 
-        if (! is_string($format)) {
+        if (!is_string($format)) {
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception('Format must be a string');
         }
@@ -62,6 +62,28 @@ class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
     }
 
     /**
+	 * Factory for Zend_Log_Formatter_Simple classe
+	 *
+	 * @param array|Zend_Config $options
+	 * @return Zend_Log_Formatter_Simple
+     */
+    public static function factory($options)
+    {
+        $format = null;
+        if (null !== $options) {
+            if ($options instanceof Zend_Config) {
+                $options = $options->toArray();
+            }
+
+            if (array_key_exists('format', $options)) {
+                $format = $options['format'];
+            }
+        }
+
+        return new self($format);
+    }
+
+    /**
      * Formats data into a single line to be written by the writer.
      *
      * @param  array    $event    event data
@@ -70,17 +92,17 @@ class Zend_Log_Formatter_Simple implements Zend_Log_Formatter_Interface
     public function format($event)
     {
         $output = $this->_format;
-        foreach ($event as $name => $value) {
 
+        foreach ($event as $name => $value) {
             if ((is_object($value) && !method_exists($value,'__toString'))
-                || is_array($value)) {
-
+                || is_array($value)
+            ) {
                 $value = gettype($value);
             }
 
             $output = str_replace("%$name%", $value, $output);
         }
+
         return $output;
     }
-
-}
+}

+ 50 - 12
library/Zend/Log/Formatter/Xml.php

@@ -20,8 +20,8 @@
  * @version    $Id$
  */
 
-/** Zend_Log_Formatter_Interface */
-require_once 'Zend/Log/Formatter/Interface.php';
+/** Zend_Log_Formatter_Abstract */
+require_once 'Zend/Log/Formatter/Abstract.php';
 
 /**
  * @category   Zend
@@ -31,7 +31,7 @@ require_once 'Zend/Log/Formatter/Interface.php';
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
  * @version    $Id$
  */
-class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
+class Zend_Log_Formatter_Xml extends Zend_Log_Formatter_Abstract
 {
     /**
      * @var string Name of root element
@@ -50,17 +50,56 @@ class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
 
     /**
      * Class constructor
+     * (the default encoding is UTF-8)
      *
-     * @param string $rootElement Name of root element
-     * @param array $elementMap Relates XML elements to log data field keys
-     * @param string $encoding Encoding to use (defaults to UTF-8)
+     * @param array|Zend_Config $options
      * @return void
      */
-    public function __construct($rootElement = 'logEntry', $elementMap = null, $encoding = 'UTF-8')
+    public function __construct($options = array())
     {
-        $this->_rootElement = $rootElement;
-        $this->_elementMap  = $elementMap;
-        $this->setEncoding($encoding);
+        if ($options instanceof Zend_Config) {
+            $options = $options->toArray();
+        } elseif (!is_array($options)) {
+            $args = func_get_args();
+
+            $options = array(
+            	'rootElement' => array_shift($args)
+            );
+
+            if (count($args)) {
+                $options['elementMap'] = array_shift($args);
+            }
+
+            if (count($args)) {
+                $options['encoding'] = array_shift($args);
+            }
+        }
+
+        if (!array_key_exists('rootElement', $options)) {
+            $options['rootElement'] = 'logEntry';
+        }
+
+        if (!array_key_exists('encoding', $options)) {
+            $options['encoding'] = 'UTF-8';
+        }
+
+        $this->_rootElement = $options['rootElement'];
+        $this->setEncoding($options['encoding']);
+
+        if (array_key_exists('elementMap', $options)) {
+            $this->_elementMap  = $options['elementMap'];
+        }
+    }
+
+    /**
+	 * Factory for Zend_Log_Formatter_Xml classe
+	 *
+	 * @param array|Zend_Config $options
+	 * @return Zend_Log_Formatter_Xml
+     */
+    public static function factory($options)
+    {
+        return new self($options);
     }
 
     /**
@@ -118,5 +157,4 @@ class Zend_Log_Formatter_Xml implements Zend_Log_Formatter_Interface
 
         return $xml . PHP_EOL;
     }
-
-}
+}

+ 10 - 0
tests/Zend/Log/Formatter/FirebugTest.php

@@ -56,6 +56,16 @@ class Zend_Log_Formatter_FirebugTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals('tottakai', $output);
     }
+
+    /**
+     * @group ZF-9176
+     */
+    public function testFactory()
+    {
+        $options = array();
+        $formatter = Zend_Log_Formatter_Firebug::factory($options);
+        $this->assertType('Zend_Log_Formatter_Firebug', $formatter);
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_FirebugTest::main') {

+ 12 - 0
tests/Zend/Log/Formatter/SimpleTest.php

@@ -111,6 +111,18 @@ class Zend_Log_Formatter_SimpleTest extends PHPUnit_Framework_TestCase
         $line = $f->format($fields);
         $this->assertContains('object', $line);
     }
+
+    /**
+     * @group ZF-9176
+     */
+    public function testFactory()
+    {
+        $options = array(
+            'format' => '%timestamp% [%priority%]: %message% -- %info%'
+        );
+        $formatter = Zend_Log_Formatter_Simple::factory($options);
+        $this->assertType('Zend_Log_Formatter_Simple', $formatter);
+    }
 }
 
 class Zend_Log_Formatter_SimpleTest_TestObject1 {

+ 37 - 0
tests/Zend/Log/Formatter/XmlTest.php

@@ -100,6 +100,43 @@ class Zend_Log_Formatter_XmlTest extends PHPUnit_Framework_TestCase
 
         $this->assertContains('&amp;amp', $line);
     }
+
+    public function testConstructorWithArray()
+    {
+        $options = array(
+            'rootElement' => 'log',
+            'elementMap' => array(
+                'word' => 'message',
+                'priority' => 'priority'
+            )
+        );
+        $event = array(
+            'message' => 'tottakai',
+            'priority' => 4
+        );
+        $expected = '<log><word>tottakai</word><priority>4</priority></log>';
+
+        $formatter = new Zend_Log_Formatter_Xml($options);
+        $output = $formatter->format($event);
+        $this->assertContains($expected, $output);
+        $this->assertEquals('UTF-8', $formatter->getEncoding());
+    }
+
+    /**
+     * @group ZF-9176
+     */
+    public function testFactory()
+    {
+        $options = array(
+            'rootElement' => 'log',
+            'elementMap' => array(
+                'timestamp' => 'timestamp',
+                'response' => 'message'
+            )
+        );
+        $formatter = Zend_Log_Formatter_Xml::factory($options);
+        $this->assertType('Zend_Log_Formatter_Xml', $formatter);
+    }
 }
 
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_XmlTest::main') {

+ 52 - 0
tests/Zend/Log/LogTest.php

@@ -451,6 +451,46 @@ class Zend_Log_LogTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('EMERG', $mock->events[0]['priorityName']);
         $this->assertFalse(array_key_exists(1, $mock->events));
     }
+
+    /**
+     * @group ZF-9176
+     */
+    public function testLogConstructFromConfigFormatter()
+    {
+        $config = array(
+        	'log' => array(
+	        	'test' => array(
+		            'writerName'    => 'Mock',
+		            'formatterName' => 'Simple',
+		            'formatterParams' => array(
+		                'format' => '%timestamp% (%priorityName%): %message%'
+		            )
+	            )
+            )
+        );
+
+        $logger = Zend_Log::factory($config['log']);
+        $logger->log('custom message', Zend_Log::INFO);
+    }
+
+	/**
+     * @group ZF-9176
+     */
+    public function testLogConstructFromConfigCustomFormatter()
+    {
+        $config = array(
+        	'log' => array(
+	        	'test' => array(
+		            'writerName'    => 'Mock',
+		            'formatterName' => 'Mock',
+        			'formatterNamespace' => 'Custom_Formatter'
+	            )
+            )
+        );
+
+        $logger = Zend_Log::factory($config['log']);
+        $logger->log('custom message', Zend_Log::INFO);
+    }
 }
 
 class Zend_Log_Writer_NotExtendedWriterAbstract implements Zend_Log_FactoryInterface
@@ -467,6 +507,18 @@ class Zend_Log_Filter_NotImplementsFilterInterface implements Zend_Log_FactoryIn
     }
 }
 
+class Custom_Formatter_Mock extends Zend_Log_Formatter_Abstract
+{
+    public static function factory($config)
+    {
+        return new self;
+    }
+
+    public function format($event)
+    {
+    }
+}
+
 if (PHPUnit_MAIN_METHOD == 'Zend_Log_LogTest::main') {
     Zend_Log_LogTest::main();
 }