Yii2 日志的源码解读

1. 组成

Yii2的logger主要分为三个部分:

  • Logger 负责日志级别,记录格式等等的配置和管理;
  • Dispatcher 负责日志的收集和对target的管理;
  • target 负责执行写入的操作,可以是写文件,写数据库等等。
    (我是觉得代码写得不是很好,Dispatcher作为Logger的成员,而Logger也作为Dispatcher的成员,我觉得耦合性太强了,monolog相对就好很多,有空再写这个源码的分析)

2. 部分源码

1. 先看application类里面注册的核心组件

\yii\base\Application::coreComponents
   /**
     * Returns the configuration of core application components.
     * @see set()
     */
    public function coreComponents()
    {
        return [
            'log' => ['class' => 'yii\log\Dispatcher'],
            'view' => ['class' => 'yii\web\View'],
            'formatter' => ['class' => 'yii\i18n\Formatter'],
            'i18n' => ['class' => 'yii\i18n\I18N'],
            'mailer' => ['class' => 'yii\swiftmailer\Mailer'],
            'urlManager' => ['class' => 'yii\web\UrlManager'],
            'assetManager' => ['class' => 'yii\web\AssetManager'],
            'security' => ['class' => 'yii\base\Security'],
        ];
    }

  • 2 初始化log组件
- 0. \yii\base\Application::getLog
- 1. \yii\log\Dispatcher::__construct 初始化logger
 /**
     * @inheritdoc
     */
    public function __construct($config = [])
    {
        // ensure logger gets set before any other config option
        if (isset($config['logger'])) {
            $this->setLogger($config['logger']);
            unset($config['logger']);
        }
        // connect logger and dispatcher
        $this->getLogger(); // 重要

        parent::__construct($config);
    }
- 2. \yii\log\Dispatcher::getLogger 创建一个logger单例
    /**
     * Gets the connected logger.
     * If not set, [[\Yii::getLogger()]] will be used.
     * @property Logger the logger. If not set, [[\Yii::getLogger()]] will be used.
     * @return Logger the logger.
     */
    public function getLogger()
    {
        if ($this->_logger === null) {
            $this->setLogger(Yii::getLogger());
        }
        return $this->_logger;
    }


- 3. 从上一步的getLogger追代码到:\yii\log\Dispatcher::setLogger
    /**
     * Sets the connected logger.
     * @param Logger $value the logger.
     */
    public function setLogger($value)
    {
        $this->_logger = $value;
        $this->_logger->dispatcher = $this; //这一步重要,把logger里的dispatcher关联了自己
    }


- 4. \yii\log\Dispatcher::init 初始化注册的target
 /**
     * @inheritdoc
     */
    public function init()
    {
        parent::init();

        foreach ($this->targets as $name => $target) {
            if (!$target instanceof Target) {
                $this->targets[$name] = Yii::createObject($target);
            }
        }
    }

关键步骤就是以上了。

    1. 打日志,调用 Yii::error.
- 1.调用 Yii::error其实就是调用\yii\BaseYii::error
   /**
     * Logs an error message.
     * An error message is typically logged when an unrecoverable error occurs
     * during the execution of an application.
     * @param string $message the message to be logged.
     * @param string $category the category of the message.
     */
    public static function error($message, $category = 'application')
    {
        static::getLogger()->log($message, Logger::LEVEL_ERROR, $category);
    }
- 2.再看getLogger
   /**
     * @return Logger message logger
     */
    public static function getLogger()
    {
        if (self::$_logger !== null) {
            return self::$_logger;
        } else {
            return self::$_logger = static::createObject('yii\log\Logger'); //返回单例
        }
    }
- 3. 注意到初始化组件的时候, \yii\log\Dispatcher::getLogger调用的就是上面的 getLogger(),
所以,这是同一个对象来的,打日志就可以用logger里面的dispatcher来管理target了,target就是执行写日志的功能。

你可能感兴趣的:(Yii2 日志的源码解读)