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就是执行写日志的功能。
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • date: 2017-11-21 18:15:18title: yii 源码解读 百度脑图 - yii 源码解析:...
    daydaygo阅读 3,600评论 1 11
  • 日志 Yii提供了一个高度自定义化和高扩展性的日志框架。根据使用场景的不同,你可以很容易的对各种消息就行记录、过滤...
    柏树_Jeff阅读 8,444评论 1 11
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 178,592评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,864评论 2 59
  • 鉴于以前出现的数据大表误更新和全表误删除操作。影响服务使用和数据安全。 为了防止线上业务出现以下3种情况影响线上服...
    tonywu阅读 3,121评论 0 1

友情链接更多精彩内容