log.php

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

PHP
551
字号


    /**
     * Sets the default logger.  If no logName is specified, then "LOG" is used.  For any
     * named logger other than "LOG", the logger must have been registered with registerLogger().
     *
     * @param  string        $logName        Name of this instance, used to access it from other static functions.
     * @return boolean       True
     */
    public static function setDefaultLogger($logName = null)
    {
        if (is_null($logName) || $logName == 'LOG') {
            $logName = 'LOG';
        } else if (!self::hasLogger($logName)) {
            throw new Zend_Log_Exception("Cannot set default, no instance of log named \"$logName\".");
        }

        self::$_defaultLogName = $logName;
        return true;
    }


    /**
     * Sets the values for log fields. Omitted fields are set to default values.
     *
     * @param  array $fields
     * @param  string $logName
     * @return boolean True
     */
    public static function setFields($fields, $logName = null)
    {
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        }

        if (!array_key_exists('message', $fields)) {
            $fields['message'] = '';
        }

        if (!array_key_exists('level', $fields)) {
            $fields['level'] = '';
        }

        self::_getInstance($logName)->_fields = $fields;
        return true;
    }


    /**
     * Returns an array of the log fields.
     *
     * @param  string $logName
     * @return array
     */
    public static function getFields($logName = null)
    {
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        }

        return self::_getInstance($logName)->_fields;
    }


    /**
     * Sends a message to the log.
     *
     * @param string $message
     * @param integer $level
     * @param mixed $logName_or_fields
     * @param string $logName
     * @throws Zend_Log_Exception
     * @return boolean True
     */
    public static function log($message, $level = self::LEVEL_DEBUG, $logName_or_fields = null, $logName = null)
    {
        // Check to see that the specified log level is actually a level
        // and not the LEVEL_ALL mask or an invalid level.
        if (!self::isLogLevel($level)) {
            throw new Zend_Log_Exception('Unknown log level specified.');
        }

        if (is_string($logName_or_fields)) {
            $logName = $logName_or_fields;
        } else {
            if (!is_null($logName_or_fields)) {
                // Fields must be specified as key=>value pairs.
                if (!is_array($logName_or_fields)) {
                    throw new Zend_Log_Exception('Optional fields must be supplied as an associative array of key/value pairs.');
                }

                /**
                 * If the first key in the $logName_or_fields array is numeric, we'll assume that this is an array
                 * that was generated by array() and as such it's an array of lognames.  Otherwise, assume fields.
                 */
                reset($logName_or_fields);
                if (is_numeric(key($logName_or_fields))) {
                    $logName = $logName_or_fields;
                    $fields = null;
                } else {
                    // Fields passed must be in the array with keys matching the keys that were set by setFields().
                    $fields = array();
                    foreach ($logName_or_fields as $fieldName => $fieldValue) {
                        $fields[$fieldName] = $fieldValue;
                    }
                }
            }
        }


        /**
         * A log may be specified or the default log will be selected.  A special logname, ZF, exists
         * only for internal logging of the framework.
         */
        if (is_null($logName)) {
            $logName = self::$_defaultLogName;
        } else {
            if ($logName == 'ZF' && !isset(self::$_instances['ZF'])) {
                self::registerLogger(new Zend_Log_Adapter_Null(), 'ZF');
            }
        }


        /**
         * For any fields that were not specified, use the defaults.
         */
        $fields['message'] = $message;
        $fields['level'] = self::$_levelNames[$level];
        foreach (self::getFields($logName) as $fieldName => $fieldValue) {
            if (!array_key_exists($fieldName, $fields)) {
                $fields[$fieldName] = $fieldValue;
            }
        }


        /**
         * If the supplied logName is actually an array of logNames, then
         * call the function recursively to post to all the logs.
         */
        if (is_array($logName)) {
            foreach ($logName as $l) {
                self::log($message, $level, $fields, $l);
            }
            return true;
        }

        // Write the message to the log if the current log level will accept it.
        /* @var $logger Zend_Log */
        $logger = self::_getInstance($logName);

        if ($level & $logger->_levelMask) {
            $fields['message'] = $logger->_messagePrefix . $message . $logger->_messageSuffix;
            $logger->_adapter->write($fields);
        }

        return true;
    }


    /**
     * Destroy all Zend_Log instances in Zend_Log::$_instances.  This is equivalent to calling unregister()
     * for each log instance.
     *
     * @return boolean True
     */
    public static function close()
    {
        // This will cause the destruction of the instances.  The destructor
        // in the Zend_Log_Adapter_File class will clean up on its way out.
        self::$_instances = null;

        return true;
    }


    /**
     * Sets a message prefix.  The prefix will be automatically prepended to any message that is
     * sent to the specified log.
     *
     * @param  string         $prefix         The prefix string
     * @param  string         $logName        Name of this instance
     * @return boolean        True
     */
    public static function setMessagePrefix($prefix, $logName = null)
    {
        self::_getInstance($logName)->_messagePrefix = $prefix;
        return true;
    }


    /**
     * Sets a message suffix.  The suffix will be automatically appended to any message that is
     * sent to the specified log.
     *
     * @param  string         $suffix         The suffix string
     * @param  string         $logName        Name of this instance
     * @return boolean        True
     */
    public static function setMessageSuffix($suffix, $logName = null)
    {
        self::_getInstance($logName)->_messageSuffix = $suffix;
        return true;
    }


    /**
     * Sets the logging level of the log instance to one of the Zend_Log::LEVEL_* constants.  Only
     * messages with this log level will be logged by the instance, all others will be ignored.
     *
     * @param  integer            $level
     * @param  string             $logName        Name of this instance
     * @throws Zend_Log_Exception
     * @return boolean            True
     */
    public static function setLevel($level, $logName = null)
    {
        if (!self::isLogLevel($level)) {
            throw new Zend_Log_Exception('Unknown log level specified.');
        }

        self::_getInstance($logName)->_levelMask = $level;
        return true;
    }


    /**
     * Sets the logging level of the log instance based on a mask.  The mask is the bitwise OR
     * of any of the Zend_Log::LEVEL_* constants.
     *
     * @param  integer            $mask           The log level mask
     * @param  string             $logName        Name of this instance
     * @throws Zend_Log_Exception
     * @return boolean            True
     */
    public static function setMask($mask, $logName = null)
    {
        if (!is_int($mask) || $mask < 0 || $mask > 255) {
            throw new Zend_Log_Exception('Level mask out of range (should be integer between 0 and 255).');
        }

        $logger = self::_getInstance($logName);
        $logger->_levelMask = $mask;
        return true;
    }


    /**
     * Sets and adapter-specific option.
     *
     * @param string    $optionKey      The option name
     * @param string    $optionValue    The option value
     * @param string    $logName        Name of this instance
     */
    public static function setAdapterOption($optionKey, $optionValue, $logName = null)
    {
        $logger = self::_getInstance($logName);
        return $logger->_adapter->setOption($optionKey, $optionValue);
    }


    /**
     * Tests if the supplied $level is one of the valid log levels (Zend_Log::LEVEL_* constants).
     *
     * @param  int     $level       Value to test
     * @return boolean              Is it a valid level?
     */
    public static function isLogLevel($level)
    {
        return in_array($level, array(self::LEVEL_SEVERE, self::LEVEL_ERROR, self::LEVEL_WARNING,
                        self::LEVEL_INFO, self::LEVEL_DEBUG));
    }

}

⌨️ 快捷键说明

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