spatie/backtrace
Composer 安装命令:
composer require spatie/backtrace
包简介
A better backtrace
关键字:
README 文档
README
To get the backtrace in PHP you can use the debug_backtrace function. By default, it can be hard to work with. The
reported function name for a frame is skewed: it belongs to the previous frame. Also, options need to be passed using a bitmask.
This package provides a better way than debug_backtrace to work with a back trace. Here's an example:
// returns an array with `Spatie\Backtrace\Frame` instances $frames = Spatie\Backtrace\Backtrace::create()->frames(); $firstFrame = $frames[0]; $firstFrame->file; // returns the file name $firstFrame->lineNumber; // returns the line number $firstFrame->class; // returns the class name
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/backtrace
Usage
This is how you can create a backtrace instance:
$backtrace = Spatie\Backtrace\Backtrace::create();
Getting the frames
To get all the frames you can call frames.
$frames = $backtrace->frames(); // contains an array with `Spatie\Backtrace\Frame` instances
A Spatie\Backtrace\Frame has these properties:
file: the name of the filelineNumber: the line numberarguments: the arguments used for this frame. Will benullifwithArgumentswas not used.class: the class name for this frame. Will benullif the frame concerns a function.method: the method used in this frameobject: the object when the frame is in an object context (method call, closure bound to object, arrow function which captured$this, etc.). Will benullifwithObjectwas not used.applicationFrame: containstrueis this frame belongs to your application, andfalseif it belongs to a file in the vendor directory. A couple edge cases exist, see "application frames" below.
Collecting arguments and objects
For performance reasons, the frames of the back trace will not contain the arguments of the called functions and the
object. If you want to add those, use the withArguments and withObject methods.
$backtrace = Spatie\Backtrace\Backtrace::create()->withArguments()->withObject();
Reducing arguments
For viewing purposes, arguments can be reduced to a string:
$backtrace = Spatie\Backtrace\Backtrace::create()->withArguments()->reduceArguments();
By default, some typical types will be reduced to a string. You can define your own reduction algorithm per type by implementing an ArgumentReducer:
class DateTimeWithOtherFormatArgumentReducer implements ArgumentReducer { public function execute($argument): ReducedArgumentContract { if (! $argument instanceof DateTimeInterface) { return UnReducedArgument::create(); } return new ReducedArgument( $argument->format('d/m/y H:i'), get_class($argument), ); } }
This is a copy of the built-in argument reducer for DateTimeInterface where we've updated the format. An UnReducedArgument object is returned when the argument is not of the expected type. A ReducedArgument object is returned with the reduced value of the argument and the original type of the argument.
The reducer can be used as such:
$backtrace = Spatie\Backtrace\Backtrace::create()->withArguments()->reduceArguments( Spatie\Backtrace\Arguments\ArgumentReducers::default([ new DateTimeWithOtherFormatArgumentReducer() ]) );
Which will first execute the new reducer and then the default ones.
Setting the application path
You can use the applicationPath to pass the base path of your app. This value will be used to determine whether a
frame is an application frame, or a vendor frame. Here's an example using a Laravel specific function.
$backtrace = Spatie\Backtrace\Backtrace::create()->applicationPath(base_path());
Removing the application path from the file name
You can use trimFilePaths to remove the base path of your app from the file. This will only work if you use it in conjunction with the applicationPath method re above. Here's an example using a Laravel specific function. This will ensure the Frame has the trimmedFilePath property set.
$backtrace = Backtrace::create()->applicationPath(base_path())->trimFilePaths());
Application frames
By default, a frame is considered an application frame when the file of the frame is not in the vendor directory. The following edge cases apply:
- Laravel's
artisanCLI file (generally found in the root of the project) is considered a vendor file and frame - Statamic's
pleaseCLI file (generally found in the root of the project) is considered a vendor file and frame
Getting a certain part of a trace
If you only want to have the frames starting from a particular frame in the backtrace you can use
the startingFromFrame method:
use Spatie\Backtrace\Backtrace; use Spatie\Backtrace\Frame; $frames = Backtrace::create() ->startingFromFrame(function (Frame $frame) { return $frame->class === MyClass::class; }) ->frames();
With this code, all frames before the frame that concerns MyClass will have been filtered out.
Alternatively, you can use the offset method, which will skip the given number of frames. In this example the first 2 frames will not end up in $frames.
$frames = Spatie\Backtrace\Backtrace::create() ->offset(2) ->frames();
Limiting the number of frames
To only get a specific number of frames use the limit function. In this example, we'll only get the first two frames.
$frames = Spatie\Backtrace\Backtrace::create() ->limit(2) ->frames();
Getting a backtrace for a throwable
Here's how you can get a backtrace for a throwable.
$frames = Spatie\Backtrace\Backtrace::createForThrowable($throwable)
Because we will use the backtrace that is already available in the throwable, the frames will contain the arguments used
in the backtrace as long as the zend.exception_ignore_args INI option is disabled (set to 0) before the throwable
is thrown. On the other hand, objects will never be included in the backtrace.
More information.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/backtrace 适用场景与选型建议
spatie/backtrace 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 182.74M 次下载、GitHub Stars 达 434, 最近一次更新时间为 2020 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Backtrace」 「spatie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/backtrace 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/backtrace 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/backtrace 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel-specific and pure PHP Helper Functions.
Receive webhooks in Laravel apps
🐛 Debug a PHP script easily by following its backtrace.
Easily optimize images using PHP
Store your application settings
Debug2 is a function developed to debug PHP code of form easy, simple, pretty print, fast and safe.
统计信息
- 总下载量: 182.74M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 439
- 点击次数: 24
- 依赖项目数: 48
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2020-11-24