码迷,mamicode.com
首页 > Web开发 > 详细

[李景山php]每天TP5-20170108|thinkphp5-Log.php

时间:2016-12-02 16:43:41      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:thinkphp

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace think;

use think\exception\ClassNotFoundException;
// 使用异常 类未捕获 异常
/**
 * Class Log
 * @package think
 *
 * @method void log($msg) static 静态 日志 记录
 * @method void error($msg) static 静态 错误 信息
 * @method void info($msg) static 静态 信息
 * @method void sql($msg) static 静态 sql 语句
 * @method void notice($msg) static 静态 通知
 * @method void alert($msg) static 静态 警告
 */
class Log
{
    const LOG    = ‘log‘;// 常量定义
    const ERROR  = ‘error‘;// 错误定义
    const INFO   = ‘info‘; // 信息定义
    const SQL    = ‘sql‘; // SQL定义
    const NOTICE = ‘notice‘;// 通知定义
    const ALERT  = ‘alert‘;// 警告定义

    // 日志信息
    protected static $log = [];// 日志 信息 存放
    // 配置参数
    protected static $config = [];// 配置参数 信息 存放
    // 日志类型
    protected static $type = [‘log‘, ‘error‘, ‘info‘, ‘sql‘, ‘notice‘, ‘alert‘];// 日志 类型
    // 日志写入驱动
    protected static $driver;// 日志 写入 驱动

    // 当前日志授权key
    protected static $key;// 日志 授权

    /**
     * 日志初始化
     * @param array $config
     */
    public static function init($config = [])// 日志 初始化
    {
        $type         = isset($config[‘type‘]) ? $config[‘type‘] : ‘File‘;// 日志记录 类型 日志存储 截至
        $class        = false !== strpos($type, ‘\\‘) ? $type : ‘\\think\\log\\driver\\‘ . ucwords($type);// 日志驱动
        self::$config = $config;// 覆盖 当前配置
        unset($config[‘type‘]);// 清除  类型
        if (class_exists($class)) {
            self::$driver = new $class($config);// 根据 类型 找到的类,然后把数据放进去
        } else {
            throw new ClassNotFoundException(‘class not exists:‘ . $class, $class);// 否则 抛出 异常
        }
        // 记录初始化信息
        App::$debug && Log::record(‘[ LOG ] INIT ‘ . $type, ‘info‘);// 日志记录, App 下面的日志记录
        // 调试 权限 用的是 App 的 App 是个类,记录日志信息
    }

    /**
     * 获取日志信息
     * @param string $type 信息类型
     * @return array
     */
    public static function getLog($type = ‘‘)// 获取日志信息,不同的日志信息类型 info error 等
    {
        return $type ? self::$log[$type] : self::$log;
    }

    /**
     * 记录调试信息
     * @param mixed  $msg  调试信息
     * @param string $type 信息类型
     * @return void
     */
    public static function record($msg, $type = ‘log‘)// 默认写入 日志 数据 仓库 而已
    {
        self::$log[$type][] = $msg;// 并没有写入文件
    }

    /**
     * 清空日志信息
     * @return void
     */
    public static function clear()// 清空 日志 存档
    {
        self::$log = [];
    }

    /**
     * 当前日志记录的授权key
     * @param string  $key  授权key
     * @return void
     */
    public static function key($key)// 授权key
    {
        self::$key = $key;
    }

    /**
     * 检查日志写入权限
     * @param array  $config  当前日志配置参数
     * @return bool
     */
    public static function check($config)// 检查日志 写入 权限
    {
        if (self::$key && !empty($config[‘allow_key‘]) && !in_array(self::$key, $config[‘allow_key‘])) {
            return false;// 日志权限 很 松散啊
        }
        return true;
    }

    /**
     * 保存调试信息
     * @return bool
     */
    public static function save()
    {// 写入日志文件
        if (!empty(self::$log)) {// 不为空
            if (is_null(self::$driver)) {// 初始化驱动
                self::init(Config::get(‘log‘));
            }

            if (!self::check(self::$config)) {// 检测权限
                // 检测日志写入权限
                return false;
            }

            if (empty(self::$config[‘level‘])) {
                // 获取全部日志
                $log = self::$log;
            } else {
                // 记录允许级别
                $log = [];
                foreach (self::$config[‘level‘] as $level) {
                    if (isset(self::$log[$level])) {
                        $log[$level] = self::$log[$level];
                    }
                }
            }

            $result = self::$driver->save($log);// 调用 策略 存入
            if ($result) {
                self::$log = [];
            }

            return $result;
        }
        return true;
    }

    /**
     * 实时写入日志信息 并支持行为
     * @param mixed  $msg  调试信息
     * @param string $type 信息类型
     * @param bool   $force 是否强制写入
     * @return bool
     */
    public static function write($msg, $type = ‘log‘, $force = false)
    {
        // 封装日志信息
        if (true === $force || empty(self::$config[‘level‘])) {// 强制 写入
            $log[$type][] = $msg;
        } elseif (in_array($type, self::$config[‘level‘])) {// 许可类型 内
            $log[$type][] = $msg;
        } else {
            return false;
        }

        // 监听log_write
        Hook::listen(‘log_write‘, $log);// 监听
        if (is_null(self::$driver)) {// 初始化
            self::init(Config::get(‘log‘));
        }
        // 写入日志
        return self::$driver->save($log);// 存储日志
    }

    /**
     * 静态调用
     * @param $method
     * @param $args
     * @return mixed
     */
    public static function __callStatic($method, $args)// 静态调用其它 特殊的数据
    {
        if (in_array($method, self::$type)) {
            array_push($args, $method);// 把函数 作为参数 存入 日志处理
            return call_user_func_array(‘\\think\\Log::record‘, $args);
        }
    }

}


本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1878757

[李景山php]每天TP5-20170108|thinkphp5-Log.php

标签:thinkphp

原文地址:http://jingshanls.blog.51cto.com/3357095/1878757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!