PHPackages                             yangweijie/think-inspector - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. yangweijie/think-inspector

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

yangweijie/think-inspector
==========================

inspector package for thinkphp

70PHP

Since Jan 10Pushed 3y ago1 watchersCompare

[ Source](https://github.com/yangweijie/think-inspector)[ Packagist](https://packagist.org/packages/yangweijie/think-inspector)[ RSS](/packages/yangweijie-think-inspector/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

 think-inspector
=================

[](#-think-inspector-)

 inspector package for thinkphp.

[![image](https://user-images.githubusercontent.com/1614114/149458950-7e009eee-619a-4f77-9192-8891a2b19ef6.png)](https://user-images.githubusercontent.com/1614114/149458950-7e009eee-619a-4f77-9192-8891a2b19ef6.png)

Installing
----------

[](#installing)

```
$ composer require yangweijie/think-inspector -vvv
```

Usage
-----

[](#usage)

### 配置

[](#配置)

#### key

[](#key)

在  注册后创建应用的 key 用于区分不同应用 前后端 应该也是独立应用

#### enable

[](#enable)

是否启用监听

#### max\_items

[](#max_items)

每个监听会话里 允许添加的最大片段数。 超过 添加不进去

#### env

[](#env)

目前没用到 计划 用于区分 不同环境的

#### single

[](#single)

是否区分 不同url 命令

true 意味着 启动监听时的path 是动态独立 ， 然后 monitor 面版 会通过这些path 去区分 不同的监听 分组 [![true](https://user-images.githubusercontent.com/1614114/149459050-30b97ce8-2f45-485c-aa00-ad42b8c2fc16.png)](https://user-images.githubusercontent.com/1614114/149459050-30b97ce8-2f45-485c-aa00-ad42b8c2fc16.png)

false web 请求 path 为 WEB 命令行 为 命令行 这样方便查找监听记录 只用 在 WEB 或 命令行里 再按时间 进行查看 [![false](https://user-images.githubusercontent.com/1614114/149459099-6ebc515c-93ab-4db4-a1c6-f5daf22f4b05.png)](https://user-images.githubusercontent.com/1614114/149459099-6ebc515c-93ab-4db4-a1c6-f5daf22f4b05.png)

### 注入

[](#注入)

在本服务中 向 app 注入了 inspector 对象 (自定义类) inspector 对象的 ins 属性 是原始 inspector-apm 的对象

inspector 可以调用 inspector-apm 里 inspector 类的方法。

为了 方便 设置 框架层的 片段 覆写了 startSegment 和 end 方法 。

只需要 `$this->app->inspector->startSegment('type', 'label');` 到 需要结束时 `$this->app->inspector->end('type');` 即可

当然如果你 业务中手动添加 监听的 片段

也可以 类似于 库里 console 覆写 doRun 方法

```
$ret   = null;
$label = sprintf("运行【%s】命令", $input->getFirstArgument());
$this->inspector->addSegment(function () use ($input, $output, &$ret){
    // trace($input);
    $ret = parent::doRun($input, $output);  // 包裹自己的代码
}, 'runCmd', $label);

```

[![效果1](https://user-images.githubusercontent.com/1614114/149459218-6676c4bd-3cb0-4cd2-b92b-3ab48e4a6117.png)](https://user-images.githubusercontent.com/1614114/149459218-6676c4bd-3cb0-4cd2-b92b-3ab48e4a6117.png)

[![效果2](https://user-images.githubusercontent.com/1614114/149459261-1a0298f8-37c1-4591-967f-c94f56eb59b5.png)](https://user-images.githubusercontent.com/1614114/149459261-1a0298f8-37c1-4591-967f-c94f56eb59b5.png)

[![效果3](https://user-images.githubusercontent.com/1614114/149459277-2566ca45-a664-4c65-a7c9-6b587dba2766.png)](https://user-images.githubusercontent.com/1614114/149459277-2566ca45-a664-4c65-a7c9-6b587dba2766.png)

#### context

[](#context)

参考 本库实现的

```
if($inspector->currentTransaction() && $inspector->currentTransaction() != null){
    $inspector->currentTransaction()->addContext('请求信息', $data);
}

```

同样 上下文 加入时 会产生多个tab 最好不用 timeline 、url 、request 系统内置的。

[![效果4](https://user-images.githubusercontent.com/1614114/149459357-517eda44-1385-4826-914c-bc8c894dc3ac.png)](https://user-images.githubusercontent.com/1614114/149459357-517eda44-1385-4826-914c-bc8c894dc3ac.png)[![效果5](https://user-images.githubusercontent.com/1614114/149459395-4776028d-8279-4205-b78c-1d65274f4b79.png)](https://user-images.githubusercontent.com/1614114/149459395-4776028d-8279-4205-b78c-1d65274f4b79.png)

### 命令行监听

[](#命令行监听)

目前 命令行 监听 需要 开启 应用 全局 Provid.php 里 配置 映射的类

```
