log.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 551 行 · 第 1/2 页

PHP
551
字号
<?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
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */


/**
 * Zend_Log_Adapter_Interface
 */
require_once 'Zend/Log/Adapter/Interface.php';

/**
 * Zend_Log_Adapter_Null
 */
require_once 'Zend/Log/Adapter/Null.php';

/**
 * Zend_Log_Exception
 */
require_once 'Zend/Log/Exception.php';


/**
 * @category   Zend
 * @package    Zend_Log
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Log
{
    /**
     * Mask that includes all log levels
     */
    const LEVEL_ALL     = 255;

    /**
     * Log levels
     */
    const LEVEL_DEBUG   = 1;
    const LEVEL_INFO    = 2;
    const LEVEL_WARNING = 4;
    const LEVEL_ERROR   = 8;
    const LEVEL_SEVERE  = 16;

    /**
     * This array contains the names of the log levels in order to support
     * logging the names of the log message level instead of its numeric value.
     *
     * @var array
     */
    static protected $_levelNames = array(
        1  => 'DEBUG',
        2  => 'INFO',
        4  => 'WARNING',
        8  => 'ERROR',
        16 => 'SEVERE'
        );

    /**
     * The static class Zend_Log holds an array of Zend_Log instances
     * in this variable that are created with registerLogger().
     *
     * @var      array
     */
    static private $_instances = array();

    /**
     * The static class Zend_Log holds an array of Zend_Log instances
     * in this variable that are created with registerLogger().
     *
     * @var      array
     */
    static private $_defaultLogName = 'LOG';

    /**
     * When this class is instantiated by registerLogger, it is
     * pushed onto the $_instances associative array.  The $_logName
     * is the key to instance in this array, and also how the user
     * will specify the instance when using the other static method
     * calls (e.g. Zend_Log::log() ).
     *
     * @var      string
     */
    protected $_logName = '';

    /**
     * Logging level mask, the bitwise OR of any of the
     * Zend_Log::LEVEL_* constants that will be logged by this
     * instance of Zend_Log.  All other levels will be ignored.
     *
     * @var      integer
     */
    protected $_levelMask = self::LEVEL_ALL;

    /**
     * Every instance of Zend_Log must contain a child object which
     * is an implementation of Zend_Log_Adapter that provides the log
     * storage.
     *
     * @var      Zend_Log_Adapter_Interface
     */
    protected $_adapter = null;

    /**
     * A string which is automatically prefixed to any message sent
     * to the Zend_Log::log() method.
     *
     * @var      string
     */
    protected $_messagePrefix = '';

    /**
     * A string which is automatically appended to any message sent
     * to the Zend_Log::log() method.
     *
     * @var      string
     */
    protected $_messageSuffix = '';

    /**
     * Array of available fields for logging
     *
     * @var array
     */
    protected $_fields = array('message' => '',
                               'level'   => '');



    /**
     * Class constructor.  Zend_Log uses the singleton pattern.  Only
     * a single Zend_Log static class may be used, however instances
     * of Zend_Log may be stored inside the Zend_Log static class by
     * calling registerLogger().
     *
     * @param string $logName Name of the Zend_Log instance, which
     * will be the key to the Zend_Log::$_instances array.
     *
     * @param Zend_Log_Adapter_Interface $adapter
     */
    private function __construct($logName, Zend_Log_Adapter_Interface $adapter)
    {
        $this->_adapter = $adapter;
        $this->_adapter->logName = $logName;
    }


    /**
     * Returns the instance of Zend_Log in the Zend_Log::$_instances
     * array.
     *
     * @param  logName $logName Key in the Zend_Log::$_instances associative array.
     * @throws Zend_Log_Exception
     * @return Zend_Log_Adapter_Interface
     */
    private static function _getInstance($logName = null)
    {
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        }

        if (!self::hasLogger($logName)) {
            throw new Zend_Log_Exception("No instance of log named \"$logName\"");
        }

        return self::$_instances[$logName];
    }


    /**
     * 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.");
        }

        return true;
    }


    /**
     * Destroys an instance of Zend_Log in the $_instances array that was added by registerLogger()
     *
     * @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 unregisterLogger($logName = null)
    {
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        }

        if (!self::hasLogger($logName)) {
            throw new Zend_Log_Exception("Cannot unregister, no instance of log named \"$logName\".");
        }

        unset(self::$_instances[$logName]);
        return true;
    }


    /**
     * Returns True if the specified logName is a registered logger.  If no logName is supplied,
     * the function returns True if at least one logger exists.
     *
     * @param   string $logName   Name of registered logger to check, or null.
     * @return  boolean           Registered logger?
     */
    public static function hasLogger($logName = null)
    {
        if (!is_null($logName)) {
            return isset(self::$_instances[$logName]);
        }

        return sizeof(self::$_instances) > 0;
    }


    /**
     * Returns information about the registered loggers.
     *
     * array(2) {
     *   ["LOG"]=>          array key is the logger name
     *   array(2) {
     *      ["adapter"]=>   string,  name of the Zend_Log_AdapterClass class
     *      ["default"]=>   boolean, is this the default logger?
     *    }
     *  }
     *
     * @return  array       Is there at least one registered logger?
     */
    public static function getLoggerInfo()
    {
        if (!self::hasLogger()) {
            return false;
        }

        $loggerInfo = array();
        foreach (self::$_instances as $logName => $logger) {
            $loggerInfo[$logName]['adapter'] = get_class($logger->_adapter);
            $loggerInfo[$logName]['default'] = ($logName == self::$_defaultLogName);
        }

        return $loggerInfo;
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?