CI框架配置socketLog 远程调试

作者:matrix 发布时间:2017年4月28日 分类:零零星星

github: https://github.com/luofei614/SocketLog

socketLog是开发api的debug神器,可以在完全不影响客户端调用的时候debug数据,非常NICE.

添加Slog.php文件

下载github项目中的Slog.php到libraries目录

common_helper.php全局函数中添加代码

/**
 * CI框架 socketLog 远程调试
 *
 * 使用:
 * slog($this->db->last_query());
 *
 * chrome安装相关插件之后控制台就能看到相关数据
 *
 * @URL https://github.com/luofei614/SocketLog
 */
use think\org\Slog;
if (!function_exists('slog')) {
    function slog($log, $type = 'log', $css = '')
    {
        require_once APPPATH . 'libraries' . DIRECTORY_SEPARATOR . 'Slog.php'; #注意这里的载入路径,请确保是上一步的操作位置
        if (is_string($type)) {
            $type = preg_replace_callback('/_([a-zA-Z])/', create_function('$matches', 'return strtoupper($matches[1]);'), $type);
            if (method_exists('\think\org\Slog', $type) || in_array($type, Slog::$log_types)) {
                return call_user_func(array('\think\org\Slog', $type), $log, $css);
            }
        }
        if (is_object($type) && 'mysqli' == get_class($type)) {
            return Slog::mysqlilog($log, $type);
        }
        if (is_resource($type) && ('mysql link' == get_resource_type($type) || 'mysql link persistent' == get_resource_type($type))) {
            return Slog::mysqllog($log, $type);
        }
        if (is_object($type) && 'PDO' == get_class($type)) {
            return Slog::pdolog($log, $type);
        }
        throw new Exception($type . ' is not SocketLog method');
    }

    //配置参数加载
    slog(array(
        'enable'=>true,//是否打印日志的开关
        'host'=>'slog.thinkphp.cn',//websocket服务器地址,默认localhost
        'optimize'=>true,//是否显示利于优化的参数,如果运行时间,消耗内存等,默认为false
        'show_included_files'=>true,//是否显示本次程序运行加载了哪些文件,默认为false
        'error_handler'=>false,//是否接管程序错误,将程序错误显示在console中,默认为false
        'force_client_ids'=>array('xxx'),//日志强制记录到配置的client_id,默认为空
        'allow_client_ids'=>array('xxx')//限制允许读取日志的client_id,默认为空,表示所有人都可以获得日志。
    ),'config');
}

配置浏览器以及接收debug的客户端

此工具是TP开发人员编写,已经内置于TP5中。
此项功能需要有服务器提供websocket服务,为了避免不必要的折腾,TP也提供了公共服务:http://slog.thinkphp.cn

  1. 打开之后获取SocketLog帐号的client_id,粘贴到上一步的配置参数加载中.

  2. 安装chrome 插件 SocketLog
    https://chrome.google.com/webstore/detail/apkmbfpihjhongonfcgdagliaglghcod

  3. SocketLog 插件配置

监听地址:ws://slog.thinkphp.cn:1229
Client_ID:上面setup1中复制的id

使用

php代码中需要debug的地方slog('data');
然后chrome任意页面 打开Console,当有请求执行到debug的地方控制台即可看到数据

图片3676-CI框架配置socketLog 远程调试