popphp/pop-debug
Composer 安装命令:
composer require popphp/pop-debug
包简介
Pop Debug Component for Pop PHP Framework
README 文档
README
Overview
pop-debug is a debugging component that can be used to hook into an application to track
certain aspects of the application's lifecycle. It can help provide insight to an application's
performance or any issues that may arise within an application.
pop-debug is a component of the Pop PHP Framework.
Install
Install pop-debug using Composer.
composer require popphp/pop-debug
Or, require it in your composer.json file
"require": {
"popphp/pop-debug" : "^3.0.0"
}
Quickstart
The basic concept of the debugger is that it works with a handler object or multiple handler objects and one storage object. The handlers are wired to listen to and track various aspects of the application and push their results to the storage object to be retrieved at a later time.
In this simple example, we can set up a generic message handler to store its triggered messages in a file.
use Pop\Debug\Debugger; use Pop\Debug\Handler\MessageHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new MessageHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); $debugger['message']->addMessage('Hey! Something happened!'); $debugger->save();
The above code will save the following output to the log folder in a CSV file:
key,handler,start,end,elapsed,type,message,context
b8c00658be2aee93703deea23e58b99f,message,1762216971.7394,,,message,Hey! Something happened!,
Handlers
There are a total of 6 available handlers. More handlers can be added, provided they implement the
Pop\Debug\Handler\HandlerInterface interface.
Exception
The exception handler captures and tracks any exceptions thrown by an application.
use Pop\Debug\Debugger; use Pop\Debug\Handler\ExceptionHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new ExceptionHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); try { throw new \Exception('Error: This is a test exception'); } catch (\Exception $e) { $debugger['exception']->addException($e); $debugger->save(); }
Memory
The memory handler captures memory usages and peak memory usage. At any point in the application,
you can call the updateMemoryUsage() and updatePeakMemoryUsage() methods to take a snapshot
of memory usage in the app at that time.
use Pop\Debug\Debugger; use Pop\Debug\Handler\MemoryHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new MemoryHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); $debugger['memory']->updateMemoryUsage(); $debugger['memory']->updatePeakMemoryUsage(); sleep(2); $debugger['memory']->updateMemoryUsage(); $debugger['memory']->updatePeakMemoryUsage(); $debugger->save();
Message
The message handler provides simple and generic messaging to record debug events from within the application:
use Pop\Debug\Debugger; use Pop\Debug\Handler\MessageHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new MessageHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); $debugger['message']->addMessage('Hey! Something happened!'); $debugger->save();
PHP
The PHP handler provides a way to take a snapshot of common PHP info and INI values:
use Pop\Debug\Debugger; use Pop\Debug\Handler\PhpHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new PhpHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); $debugger->save();
Query
The query handler is a special handler that ties into the pop-db component and the profiler
available with that component. It allows you to capture any database queries and any information
associated with them.
You can set up the query handler like this:
use Pop\Debug\Debugger; use Pop\Debug\Storage\File; use Pop\Db\Db; use Pop\Db\Record; $db = Db::mysqlConnect([ 'database' => 'DATABASE', 'username' => 'DB_USER', 'password' => 'DB_PASS' ]); class Users extends Record {} Record::setDb($db); // Register the query handler with the DB adapter $queryHandler = $db->listen('Pop\Debug\Handler\QueryHandler'); $debugger = new Debugger(); $debugger->addHandler($queryHandler); $debugger->setStorage(new File('log')); // Interact with the database $user = new Users([ 'username' => 'admin', 'password' => 'password' ]); $user->save(); $debugger->save();
Request
The request handler works with a Pop\Http\Server\Request object from the pop-http component and tracks
all of the inbound request data. The following example would be a block of code that would run in a script
that receives an inbound HTTP request:
use Pop\Debug\Debugger; use Pop\Debug\Handler\RequestHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new RequestHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); $debugger->save();
Time
The time handler provides a simple way to track how long a application request takes, which is useful for performance metrics.
use Pop\Debug\Debugger; use Pop\Debug\Handler\TimeHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new TimeHandler()); $debugger->setStorage(new File(__DIR__ . '/log')); sleep(2); $debugger->save();
Storage
There are two different storage options are available to store the output of the debugger:
- CSV (or TSV) File
- Database Table
File
Store the debugger output into a file in a folder location:
use Pop\Debug\Debugger; use Pop\Debug\Handler\TimeHandler; use Pop\Debug\Storage\File; $debugger = new Debugger(); $debugger->addHandler(new TimeHandler()); $debugger->setStorage(new File(__DIR__ . '/log'));
Database
Store the debugger output into a table in a database. The default table name is pop_debug but that
can be changed with the database storage object.
use Pop\Debug\Debugger; use Pop\Debug\Handler\TimeHandler; use Pop\Debug\Storage\Database; use Pop\Db\Db; $db = Db::mysqlConnect([ 'database' => 'DATABASE', 'username' => 'DB_USER', 'password' => 'DB_PASS' ]); $debugger = new Debugger(); $debugger->addHandler(new TimeHandler()); $debugger->setStorage(new Database($db, 'text', 'my_debug_table'));
Logging
The debug component can also work with the pop-log component to deliver syslog-compatible logging messages
to a logging resource using the standard BSD syslog protocol RFC-3164.
Logging can be used in additional to the storage adapters, or by itself, sending the debug data and information
to the logging resource only and without storing anything to a storage adapter.
To work with a logger, a logger object must be passed to the debugger, along with logging parameters, which is an array
of options. The minimum parameter required is a level value. The context option can also be used to log the body
of the debug messaging results:
use Pop\Debug\Debugger; use Pop\Debug\Handler\ExceptionHandler; use Pop\Debug\Storage\File; use Pop\Log; $debugger = new Debugger(); $debugger->addHandler(new ExceptionHandler(true)); $debugger->addLogger( new Log\Logger(new Log\Writer\File(__DIR__ . '/log/debug.log')), [ 'level' => Log\Logger::ERROR, 'context' => 'json' ] ); try { throw new Pop\Debug\Exception('This is a test debug exception'); } catch (\Exception $e) { $debugger['exception']->addException($e); $debugger->save(); }
Other logging parameters options include:
Memory
The usage_limit and peak_limit are memory-specific limits to monitor is an operation goes above the specified limits.
$loggingParams = [ 'level' => Log\Logger::WARNING, 'usage_limit' => '500000', // Limit in bytes. // If the usage goes above the limit, // the log message is sent 'peak_limit' => '1000000', // Limit in bytes. // If the peak usage goes above the limit, // the log message is sent ];
Query, Request & Time
The limit parameter is supported for the query, request and time handlers. It is a time limit. If any of those
operations take longer than the time limit, a log message is sent.
$loggingParams = [ 'level' => Log\Logger::WARNING, 'limit' => 1, // Time limit in seconds. // If the operation takes longer than the time limit, // the log message is sent ];
Query Example:
use Pop\Debug\Debugger; use Pop\Db\Db; use Pop\Db\Record; use Pop\Db\Adapter\Profiler\Profiler; use Pop\Log; $db = Db::mysqlConnect([ 'database' => 'DATABASE', 'username' => 'DB_USER', 'password' => 'DB_PASS' ]); class Users extends Record {} Record::setDb($db); // Register the debugger and query handler with the DB adapter $debugger = new Debugger(); $db->listen('Pop\Debug\Handler\QueryHandler', null, new Profiler($debugger)); // Add logger to the debugger $debugger->addLogger( new Log\Logger(new Log\Writer\File(__DIR__ . '/log/debug.log')), [ 'level' => Log\Logger::INFO, 'limit' => 1 ] ); // Save a user to the database - debugging and logging will automatically happen $user = new Users([ 'username' => 'testuser', 'password' => 'password', 'email' => 'testuser@test.com' ]); $user->save();
popphp/pop-debug 适用场景与选型建议
popphp/pop-debug 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.68k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2017 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「debugger」 「pop」 「pop php」 「php debugger」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-debug 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-debug 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-debug 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This bundle lets you use the Tracy's debug screen in combination with the the default profiler in your Symfony application.
A fork of runcmf/runtracy
Pop Http Component for Pop PHP Framework
Pop PHP Framework, a lightweight, robust PHP framework
Use Google Cloud Logger into your Yii projects
Pop Log Component for Pop PHP Framework
统计信息
- 总下载量: 12.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2017-08-28