debughub / php-client
PHP 的 Debughub 客户端
README
Debughub 通过记录您应用程序的所有请求并在 debughub.com 应用程序中显示它们来帮助您的开发过程。它帮助您更快地找到代码中的问题,并在开发过程中发现性能问题。
安装
此软件包适用于纯 PHP。对于 Laravel 安装,请访问 debughub 的 Laravel 仓库。
-
在 composer.json 的
require
部分添加软件包"debughub/php-client": "0.1.*"
-
在配置目录中创建一个新的配置文件,内容如下
<?php
return [
'api_key' => '', // Your API key
'project_key' => '', // Your project key
'git_root' => '', // (Optional) Path to your git root folder. Used to track git branches
'enabled' => true, // If debughub.com client is enabled.
'send_query_data' => true, // If you want to send the data insterted into DB queries to debughub.com.
'blacklist_params' => [ // post and get params, that should never be sent to debughub.com
'password'
]
];
- 在您的代码中某处初始化 debughub 客户端:
$debughub = new \Debughub\Clients\Php\Debughub('path/to/config');
请确保也导入 composer 的自动加载。从现在起,debughub 客户端将发送请求和响应信息到 debughub.com
可用的额外方法
您可以使用这些方法添加有关您应用程序的额外信息。您可以使用 debughub 记录查询和日志。所有请求和响应信息都将自动发送。
数据库查询
要记录查询,请使用 $debughub->query(string $query, array $data, string $duration, string $connection)
方法。例如,如果您使用一个类进行所有数据库查询,您只需将此方法添加到调用查询的方法中即可。或者,您也可以将其添加到要记录的查询之后。
$query
- 要执行的查询。您可以在查询中添加一个 ?
来代替任何要替换的查询参数。例如 SELECT * FROM Users where id = ?;
并在数据变量中发送用户的 ID。
$data
- 应替换代码中的 ?
的数据数组。如果您在配置中禁用了 send_query_data
,则不会发送此数据。
$duration
- 执行查询所花费的秒数。
$connection
- 数据库连接的名称。如果您应用程序使用多个连接,则很有用。
示例
$db = DB::select("SELECT * FROM USERS"); // some imagined DB class
$debughub->query($db->getQuery(), $db->getData(), $db->getDuration(), 'some connection'); // note - the DB class and its methods are just as a example. the DB class is not part of debughub
或者,您可以让 debughub 也计算持续时间,如下所示
$query = "SELECT * FROM USERS";
$debughubQueryIndex = $debughub->startQuery($query, [], 0, 'some connection'); // note - the DB class and its methods are just as a example. the DB class is not part of debughub
$db = DB::select(query); // some imagined DB class
$debughub->endQuery($debughubQueryIndex); // you dont have to get or pass the query index, if it is not passed, the debughub will assume it was the last query you want to update with endQuery() method.
记录
要添加简单的日志,请使用 $debughub->log(string $data, string $type)
方法。日志的工作方式与查询类似。您可以使用 log() 方法记录单个操作,例如。
$data
- 要记录的数据。它可以是有任何数据类型,数组和对象将被序列化。
$type
(默认为 'info')- 日志的名称。这将改变日志在 debughub.com 应用程序中显示的方式。它应该是 info
、error
或 warning
。
$debughub->log('something just happened', 'info');
默认情况下,日志没有持续时间,在 debughub 的时间轴中仅显示为一条小线。如果您想测量某物的持续时间,例如对外部 API 的 API 调用,您可以使用
$debughubLogIndex = $debughub->startLog('something just happened', 'info');
//do some API call or something
$debughub->endLog(debughubLogIndex); // you dont have to get or pass the log index, if it is not passed, the debughub will assume it was the last log you want to update with endLog() method.
路由
如果您想将路由添加到日志中,可以使用 $debughub->route(string $route)
方法。例如
$debughub->route('users/view/{id}');