ZF-32: Zend Log throwing an error without a default logger (TRAC#16)

Description

http://framework.zend.com/developer/ticket/16

Zend Log throws an error if you register all log adapters with a name (or without a default name to be exact).

Copy and paste from the manual - section 9.4:


require_once 'Zend/Log.php';                 // Zend_Log base class
require_once 'Zend/Log/Adapter/File.php';    // File log adapter
require_once 'Zend/Log/Adapter/Console.php'; // Console log adapter

Zend_Log::registerLogger(new Zend_Log_Adapter_File('framework.txt'), 'File');
Zend_Log::registerLogger(new Zend_Log_Adapter_Console(), 'Console');

Zend_Log::log('First logging message.');

Results in:

Fatal error: Uncaught exception 'Zend_Log_Exception' with message 'No instance of log named "LOG".' in /home/martel/WWW/ZendFramework/library/Zend/Log.php:151

It think it would be far better to register the first log adapter as a default one instead of using hard-wired default named "LOG".

Comments

I have added a simple patch for this issue.

All I have done is make the first logger registered the default logger.

The manual code should now work.


/**
     * Instantiates a new instance of Zend_Log carrying the supplied Zend_Log_Adapter_Interface and stores
     * it in the $_instances array.
     *
     * @param  Zend_Log_Adapter_Interface $logAdapter Log adapter implemented from Zend_Log_Adapter_Interface
     * @param  string                     $logName    Name of this instance, used to access it from other static functions.
     * @throws Zend_Log_Exception
     * @return boolean                    True
     */
    public static function registerLogger(Zend_Log_Adapter_Interface $logAdapter, $logName=null)
    {
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        }

        /* @var $log Zend_Log */
        if (!self::hasLogger($logName)) {
            self::$_instances[$logName] = new Zend_Log($logName, $logAdapter);
        } else {
            throw new Zend_Log_Exception("Cannot register, \"$logName\" already exists.");
        }

        //make sure we have a default logger
        if(sizeof(self::$_instances == 1)) {
            self::setDefaultLogger($logName);
        }

        return true;
    }

The attached patch is invalid.

This is more a doc issue then anything else, even if the patch worked it assumes the user isn't using the default method and cause confusion.

If a developer is going to setup 2 distinct loggers they need to decide which one to use by default.

More a doc/use issue then anything else, suggested fix would break backwards compat without providing any real benifit.