2
0
Prechádzať zdrojové kódy

[REVIEW]
- Fixed array merging of options in log writers and filters
- Added log bootstrap resource that consumes Zend_Log::factory()
- Removed ZendMonitor resource in favor of Log resource
- Updated Zend_Tool ZF context; configures ZendMonitor logger; ErrorController
consumes Log resource to log exceptions

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19555 44c647ce-9c0f-0410-b52a-842ac1e357ba

matthew 16 rokov pred
rodič
commit
f4f8a50e1d

+ 35 - 0
documentation/manual/en/module_specs/Zend_Application-AvailableResources-Log.xml

@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Reviewed: no -->
+<sect2 id="zend.application.available-resources.log">
+    <title>Zend_Application_Resource_Log</title>
+
+    <para>
+        <classname>Zend_Application_Resource_Log</classname> to instantiate a
+        <classname>Zend_Log</classname> instance with an arbitrary number of log writers.
+        Configuration will be passed to the <classname>Zend_Log::factory()</classname> method,
+        allowing you to specify combinations of log writers and filters. The log instance may then
+        be retrieved from the bootstrap later in order to log events.
+    </para>
+
+    <example id="zend.application.available-resources.log.configExample">
+        <title>Sample Log Resource Configuration</title>
+
+        <para>
+            Below is a sample <acronym>INI</acronym> snippet showing how to
+            configure the log resource.
+        </para>
+
+        <programlisting language="ini"><![CDATA[
+resources.log.stream.writerName = "Stream"
+resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
+resources.log.stream.writerParams.mode = "a"
+resources.log.stream.filterName = "Priority"
+resources.log.stream.filterParams.priority = 4
+]]></programlisting>
+
+        <para>
+            For more information on available options, please review the <link
+                linkend="zend.log.factory">Zend_Log::factory() documentation</link>.
+        </para>
+    </example>
+</sect2>

+ 0 - 47
documentation/manual/en/module_specs/Zend_Application-AvailableResources-ZendMonitor.xml

@@ -1,47 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!-- Reviewed: no -->
-<sect2 id="zend.application.available-resources.zendmonitor">
-    <title>Zend_Application_Resource_Zendmonitor</title>
-
-    <para>
-        <classname>Zend_Application_Resource_Zendmonitor</classname> is a resource that instantiates
-        a <classname>Zend_Log</classname> instance with an attached <link
-            linkend="zend.log.writers.zendmonitor">Zend_Log_Writer_ZendMonitor</link> writer. It can
-        then be used within your application to log events to Zend Server's event monitor.
-    </para>
-
-    <para>
-        It has no configuration parameters; as such, you will simply need to specify it within your
-        configuration. Examples include:
-    </para>
-
-    <programlisting language="dosini"><![CDATA[
-; INI configuration:
-resources.zendmonitor[] =
-]]></programlisting>
-
-    <programlisting language="xml"><![CDATA[
-<!-- XML configuration -->
-<resources>
-    <zendmonitor />
-</resources>
-]]></programlisting>
-
-    <para>
-        Within your application, you can retrieve the logger from your bootstrap object:
-    </para>
-
-    <programlisting language="php"><![CDATA[
-$bootstrap->bootstrap('Zendmonitor');
-$log = $bootstrap->getResource('Zendmonitor');
-$log->info('some message');
-]]></programlisting>
-
-    <para>
-        The default project structure generated by the <command>zf.sh</command> or
-        <command>zf.bat</command> commands will configure the resource by default, and adds code
-        into the <classname>ErrorController</classname> to log application exceptions to the Zend
-        Server event monitor. You can disable this functionality simply by removing the resource
-        configuration from your configuration file.
-    </para>
-</sect2>

+ 1 - 1
documentation/manual/en/module_specs/Zend_Application-AvailableResources.xml

@@ -11,10 +11,10 @@
     <xi:include href="Zend_Application-AvailableResources-Db.xml" />
     <xi:include href="Zend_Application-AvailableResources-Frontcontroller.xml" />
     <xi:include href="Zend_Application-AvailableResources-Layout.xml" />
+    <xi:include href="Zend_Application-AvailableResources-Log.xml" />
     <xi:include href="Zend_Application-AvailableResources-Modules.xml" />
     <xi:include href="Zend_Application-AvailableResources-Navigation.xml" />
     <xi:include href="Zend_Application-AvailableResources-Router.xml" />
     <xi:include href="Zend_Application-AvailableResources-Session.xml" />
     <xi:include href="Zend_Application-AvailableResources-View.xml" />
-    <xi:include href="Zend_Application-AvailableResources-ZendMonitor.xml" />
 </sect1>

+ 0 - 22
documentation/manual/en/module_specs/Zend_Log-Factory.xml

@@ -44,28 +44,6 @@ $logger = Zend_Log::factory(array(
     </para>
 
     <para>
-        If you plan on only providing a single writer, you can use a slightly more compact syntax:
-    </para>
-
-    <programlisting language="php"><![CDATA[
-$logger = Zend_Log::factory(array(
-    'writerName'   => 'Stream',
-    'writerParams' => array(
-        'stream'   => '/tmp/zend.log',
-    ),
-    'filterName'   => 'Priority',
-    'filterParams' => array(
-        'priority' => Zend_Log::WARN,
-    ),
-));
-]]></programlisting>
-
-    <para>
-        We recommend using embedded arrays/configuration, however, as this provides more flexibility
-        later should you need to add log writers.
-    </para>
-
-    <para>
         Each writer can be defined with the following keys:
     </para>
 

+ 12 - 6
documentation/manual/en/module_specs/Zend_Log-Writers-ZendMonitor.xml

@@ -103,12 +103,18 @@ $log->info('Exception occurred', array(
         <title>Integration with Zend_Application</title>
 
         <para>
-            The <classname>ZendMonitor</classname> log writer has a corresponding <link
-                linkend="zend.application.available-resources.zendmonitor"><classname>Zend_Application</classname>
-                bootstrap resource</link>. If you create your
-            project using the <command>zf.sh</command> or <command>zf.bat</command> command, this
-            resource will be registered for you by default, and used in your
-            <classname>ErrorController</classname> to log application exceptions.
+            By default, the <command>zf.sh</command> and <command>zf.bat</command> commands add
+            configuration for the <link
+                linkend="zend.application.available-resources.log"><classname>Zend_Application</classname>
+                log resource</link>, which includes configuration for the
+            <classname>ZendMonitor</classname> log writer. Additionally, the
+            <classname>ErrorController</classname> uses the configured logger to log application
+            exceptions -- providing you with Zend Monitor event integration by default.
+        </para>
+
+        <para>
+            As noted previously, if the Monitor API is not detected in your PHP installation, the
+            logger will simply act as a null logger.
         </para>
     </note>
 </sect2>

+ 19 - 21
library/Zend/Application/Resource/Zendmonitor.php → library/Zend/Application/Resource/Log.php

@@ -21,14 +21,17 @@
  */
 
 /**
+ * Resource for initializing the locale
+ *
+ * @uses       Zend_Application_Resource_ResourceAbstract
  * @category   Zend
  * @package    Zend_Application
  * @subpackage Resource
  * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id$
  */
-class Zend_Application_Resource_Zendmonitor extends Zend_Application_Resource_ResourceAbstract
+class Zend_Application_Resource_Log
+    extends Zend_Application_Resource_ResourceAbstract
 {
     /**
      * @var Zend_Log
@@ -36,8 +39,8 @@ class Zend_Application_Resource_Zendmonitor extends Zend_Application_Resource_Re
     protected $_log;
 
     /**
-     * Initialize log resource
-     * 
+     * Defined by Zend_Application_Resource_Resource
+     *
      * @return Zend_Log
      */
     public function init()
@@ -46,29 +49,24 @@ class Zend_Application_Resource_Zendmonitor extends Zend_Application_Resource_Re
     }
 
     /**
-     * Get log instance
-     *
-     * Lazy-loads instance if not registered
-     * 
-     * @return Zend_Log
-     */
-    public function getLog()
-    {
-        if (null === $this->_log) {
-            $this->setLog(new Zend_Log(new Zend_Log_Writer_ZendMonitor()));
-        }
-        return $this->_log;
-    }
-
-    /**
-     * Set log instance
+     * Attach logger
      * 
      * @param  Zend_Log $log 
-     * @return Zend_Application_Resource_Zendmonitor
+     * @return Zend_Application_Resource_Log
      */
     public function setLog(Zend_Log $log)
     {
         $this->_log = $log;
         return $this;
     }
+
+    public function getLog()
+    {
+        if (null === $this->_log) {
+            $options = $this->getOptions();
+            $log = Zend_Log::factory($options);
+            $this->setLog($log);
+        }
+        return $this->_log;
+    }
 }

+ 6 - 1
library/Zend/Log.php

@@ -100,7 +100,8 @@ class Zend_Log
         }
 
         if (!is_array($config) || empty($config)) {
-            require_once 'Zend_Log_Exception.php';
+            /** @see Zend_Log_Exception */
+            require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception('Configuration must be an array or instance of Zend_Config');
         }
 
@@ -129,6 +130,7 @@ class Zend_Log
         $writer = $this->_constructFromConfig('writer', $config, $this->_defaultWriterNamespace);
 
         if (!$writer instanceof Zend_Log_Writer_Abstract) {
+            /** @see Zend_Log_Exception */
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception("{$writerName} does not extend Zend_Log_Writer_Abstract!");
         }
@@ -152,6 +154,7 @@ class Zend_Log
         $filter = $this->_constructFromConfig('filter', $config, $this->_defaultFilterNamespace);
 
         if (!$filter instanceof Zend_Log_Filter_Interface) {
+            /** @see Zend_Log_Exception */
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception("{$filterName} does not implement Zend_Log_Filter_Interface");
         }
@@ -189,6 +192,7 @@ class Zend_Log
 
         $reflection = new ReflectionClass($className);
         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.'
             );
@@ -398,6 +402,7 @@ class Zend_Log
         }
 
         if (!$writer instanceof Zend_Log_Writer_Abstract) {
+            /** @see Zend_Log_Exception */
             require_once 'Zend/Log/Exception.php';
             throw new Zend_Log_Exception(
                 'Writer must be an instance of Zend_Log_Writer_Abstract'

+ 3 - 1
library/Zend/Log/Filter/Message.php

@@ -63,7 +63,9 @@ class Zend_Log_Filter_Message extends Zend_Log_Filter_Abstract
     static public function factory($config) 
     {
         $config = self::_parseConfig($config);
-        $config = $config + array('regexp' => null);
+        $config = array_merge(array(
+            'regexp' => null
+        ), $config);
 
         return new self(
             $config['regexp']

+ 2 - 2
library/Zend/Log/Filter/Priority.php

@@ -72,10 +72,10 @@ class Zend_Log_Filter_Priority extends Zend_Log_Filter_Abstract
     static public function factory($config) 
     {
         $config = self::_parseConfig($config);
-        $config = $config + array(
+        $config = array_merge(array(
             'priority' => null, 
             'operator' => null,
-        );
+        ), $config);
 
         // Add support for constants
         if (is_string($config['priority'])) {

+ 2 - 2
library/Zend/Log/Writer/Db.php

@@ -76,11 +76,11 @@ class Zend_Log_Writer_Db extends Zend_Log_Writer_Abstract
     static public function factory($config)
     {
         $config = self::_parseConfig($config);
-        $config = $config + array(
+        $config = array_merge(array(
             'db'        => null, 
             'table'     => null, 
             'columnMap' => null,
-        );
+        ), $config);
         
         if (isset($config['columnmap'])) {
             $config['columnMap'] = $config['columnmap'];

+ 2 - 2
library/Zend/Log/Writer/Stream.php

@@ -92,10 +92,10 @@ class Zend_Log_Writer_Stream extends Zend_Log_Writer_Abstract
     static public function factory($config)
     {
         $config = self::_parseConfig($config);
-        $config = $config + array(
+        $config = array_merge(array(
             'stream' => null, 
             'mode'   => null,
-        );
+        ), $config);
 
         $streamOrUrl = isset($config['url']) ? $config['url'] : $config['stream']; 
         

+ 1 - 1
library/Zend/Tool/Project/Context/Zf/ApplicationConfigFile.php

@@ -92,7 +92,7 @@ bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
 bootstrap.class = "Bootstrap"
 resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
 resources.frontController.params.displayExceptions = 0
-resources.zendmonitor[] =
+resources.log.zendmonitor.writerName = "ZendMonitor"
 
 [staging : production]
 

+ 6 - 6
library/Zend/Tool/Project/Context/Zf/ControllerFile.php

@@ -149,9 +149,9 @@ switch (\$errors->type) {
         break;
 }
 
-// Log to Zend Monitor, if logger available
-if (\$monitor = \$this->getMonitorLog()) {
-    \$monitor->crit(\$this->view->message, \$errors->exception);
+// Log exception, if logger available
+if (\$log = \$this->getLog()) {
+    \$log->crit(\$this->view->message, \$errors->exception);
 }
 
 // conditionally display exceptions
@@ -163,13 +163,13 @@ if (\$this->getInvokeArg('displayExceptions') == true) {
 EOS
                                 )),
                             new Zend_CodeGenerator_Php_Method(array(
-                                'name' => 'getMonitorLog',
+                                'name' => 'getLog',
                                 'body' => <<<EOS
 \$bootstrap = \$this->getInvokeArg('bootstrap');
-if (!\$bootstrap->hasPluginResource('ZendMonitor')) {
+if (!\$bootstrap->hasPluginResource('Log')) {
     return false;
 }
-\$log = \$bootstrap->getResource('ZendMonitor');
+\$log = \$bootstrap->getResource('Log');
 return \$log;
 EOS
                                 )),

+ 2 - 2
tests/Zend/Application/Resource/AllTests.php

@@ -35,11 +35,11 @@ require_once 'Zend/Application/Resource/DojoTest.php';
 require_once 'Zend/Application/Resource/FrontcontrollerTest.php';
 require_once 'Zend/Application/Resource/LayoutTest.php';
 require_once 'Zend/Application/Resource/LocaleTest.php';
+require_once 'Zend/Application/Resource/LogTest.php';
 require_once 'Zend/Application/Resource/ModulesTest.php';
 require_once 'Zend/Application/Resource/NavigationTest.php';
 require_once 'Zend/Application/Resource/SessionTest.php';
 require_once 'Zend/Application/Resource/ViewTest.php';
-require_once 'Zend/Application/Resource/ZendmonitorTest.php';
 
 /**
  * @category   Zend
@@ -67,11 +67,11 @@ class Zend_Application_Resource_AllTests
         $suite->addTestSuite('Zend_Application_Resource_FrontcontrollerTest');
         $suite->addTestSuite('Zend_Application_Resource_LayoutTest');
         $suite->addTestSuite('Zend_Application_Resource_LocaleTest');
+        $suite->addTestSuite('Zend_Application_Resource_LogTest');
         $suite->addTestSuite('Zend_Application_Resource_ModulesTest');
         $suite->addTestSuite('Zend_Application_Resource_NavigationTest');
         $suite->addTestSuite('Zend_Application_Resource_SessionTest');
         $suite->addTestSuite('Zend_Application_Resource_ViewTest');
-        $suite->addTestSuite('Zend_Application_Resource_ZendmonitorTest');
 
         return $suite;
     }

+ 134 - 0
tests/Zend/Application/Resource/LogTest.php

@@ -0,0 +1,134 @@
+<?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_Application
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id$
+ */
+
+if (!defined('PHPUnit_MAIN_METHOD')) {
+    define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_LogTest::main');
+}
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../TestHelper.php';
+
+/**
+ * Zend_Loader_Autoloader
+ */
+require_once 'Zend/Loader/Autoloader.php';
+
+/**
+ * @category   Zend
+ * @package    Zend_Application
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Application
+ */
+class Zend_Application_Resource_LogTest extends PHPUnit_Framework_TestCase
+{
+    public static function main()
+    {
+        $suite  = new PHPUnit_Framework_TestSuite(__CLASS__);
+        $result = PHPUnit_TextUI_TestRunner::run($suite);
+    }
+
+    public function setUp()
+    {
+        // Store original autoloaders
+        $this->loaders = spl_autoload_functions();
+        if (!is_array($this->loaders)) {
+            // spl_autoload_functions does not return empty array when no
+            // autoloaders registered...
+            $this->loaders = array();
+        }
+
+        Zend_Loader_Autoloader::resetInstance();
+        $this->autoloader = Zend_Loader_Autoloader::getInstance();
+        $this->application = new Zend_Application('testing');
+        $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
+
+        Zend_Controller_Front::getInstance()->resetInstance();
+    }
+
+    public function tearDown()
+    {
+        // Restore original autoloaders
+        $loaders = spl_autoload_functions();
+        foreach ($loaders as $loader) {
+            spl_autoload_unregister($loader);
+        }
+
+        foreach ($this->loaders as $loader) {
+            spl_autoload_register($loader);
+        }
+
+        // Reset autoloader instance so it doesn't affect other tests
+        Zend_Loader_Autoloader::resetInstance();
+    }
+
+    public function testInitializationInitializesLogObject()
+    {
+        $resource = new Zend_Application_Resource_Log(array());
+        $resource->setBootstrap($this->bootstrap);
+        $resource->setOptions(array(
+            'Mock' => array('writerName' => 'Mock'),
+        ));
+        $resource->init();
+        $this->assertTrue($resource->getLog() instanceof Zend_Log);
+    }
+
+    public function testInitializationReturnsLogObject()
+    {
+        $resource = new Zend_Application_Resource_Log(array());
+        $resource->setBootstrap($this->bootstrap);
+        $resource->setOptions(array(
+            'Mock' => array('writerName' => 'Mock'),
+        ));
+        $test = $resource->init();
+        $this->assertTrue($test instanceof Zend_Log);
+    }
+
+    public function testOptionsPassedToResourceAreUsedToInitializeLog()
+    {
+        $stream = fopen('php://memory', 'w+', false);
+        $options = array('memory' => array(
+            'writerName'   => 'Stream',
+            'writerParams' => array(
+                'stream' => $stream,
+            )
+        ));
+
+        $resource = new Zend_Application_Resource_Log($options);
+        $resource->setBootstrap($this->bootstrap);
+        $resource->init();
+
+        $log      = $resource->getLog();
+        $this->assertTrue($log instanceof Zend_Log);
+
+        $log->log($message = 'logged-message', Zend_Log::INFO);
+        rewind($stream);
+        $this->assertContains($message, stream_get_contents($stream));
+    }
+}
+
+if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {
+    Zend_Application_Resource_LogTest::main();
+}

+ 0 - 83
tests/Zend/Application/Resource/ZendmonitorTest.php

@@ -1,83 +0,0 @@
-<?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_Application
- * @subpackage UnitTests
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id$
- */
-
-require_once dirname(__FILE__) . '/../../../TestHelper.php';
-
-/** Zend_Application_Resource_Resource */
-require_once 'Zend/Application/Resource/Resource.php';
-
-/** Zend_Application_Resource_ResourceAbstract */
-require_once 'Zend/Application/Resource/ResourceAbstract.php';
-
-/** Zend_Application_Resource_Zendmonitor */
-require_once 'Zend/Application/Resource/Zendmonitor.php';
-
-/** Zend_Log */
-require_once 'Zend/Log.php';
-
-/** Zend_Log_Writer_ZendMonitor */
-require_once 'Zend/Log/Writer/ZendMonitor.php';
-
-/** Zend_Log_Writer_Mock */
-require_once 'Zend/Log/Writer/Mock.php';
-
-/**
- * @category   Zend
- * @package    Zend_Application
- * @subpackage UnitTests
- * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
- * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @group      Zend_Application_Resource
- */
-class Zend_Application_Resource_ZendmonitorTest extends PHPUnit_Framework_TestCase
-{
-    public function setUp()
-    {
-        $this->resource = new Zend_Application_Resource_Zendmonitor();
-    }
-
-    public function testGetLogLazyLoadsLog()
-    {
-        $log = $this->resource->getLog();
-        $this->assertTrue($log instanceof Zend_Log);
-    }
-
-    public function testInitReturnsLogInstance()
-    {
-        $log = $this->resource->init();
-        $this->assertTrue($log instanceof Zend_Log);
-    }
-
-    public function testInitReturnsSameLogInstanceAsGetter()
-    {
-        $log = $this->resource->getLog();
-        $this->assertSame($log, $this->resource->init());
-    }
-
-    public function testSetterWillOverwriteExistingLogInstance()
-    {
-        $existing = $this->resource->getLog();
-        $this->resource->setLog($log = new Zend_Log(new Zend_Log_Writer_Mock()));
-        $this->assertNotSame($existing, $this->resource->getLog());
-        $this->assertSame($log, $this->resource->getLog());
-    }
-}