tourze/backtrace-helper
Composer 安装命令:
composer require tourze/backtrace-helper
包简介
Backtrace helper
README 文档
README
[]
(https://packagist.org/packages/tourze/backtrace-helper)
[
]
(https://packagist.org/packages/tourze/backtrace-helper)
[
]
(https://packagist.org/packages/tourze/backtrace-helper)
[
]
(https://packagist.org/packages/tourze/backtrace-helper)
[
]
(https://codecov.io/gh/tourze/backtrace-helper)
A PHP package for enhanced backtrace handling and exception printing with useful context information, built on top of Spatie's backtrace library.
Table of Contents
Features
- Enhanced Stack Traces: Clean and readable stack trace formatting with intelligent frame filtering
- Production-Ready: Automatic filtering of irrelevant stack frames in production environments (autoload, vendor internals)
- Exception Printing: Rich exception formatting with method arguments, file locations, and context
- Context-Aware Exceptions: Support for exceptions with contextual data through interfaces and traits
- Proxy Class Handling: Clean formatting for AOP and Doctrine proxy class names
- Zero Configuration: Works out of the box with sensible defaults
- PHP 8.1+ Support: Modern PHP features and type safety
Installation
composer require tourze/backtrace-helper
Requirements
- PHP 8.1 or higher
- ext-mbstring
- spatie/backtrace ^1.7.1
- symfony/event-dispatcher ^6.4
- symfony/http-kernel ^6.4
Quick Start
Basic Backtrace
<?php use Tourze\BacktraceHelper\Backtrace; // Create a backtrace at the current point $backtrace = Backtrace::create(); // Get as string echo $backtrace->toString(); // Get as array $frames = $backtrace->frames();
Exception Printing
<?php use Tourze\BacktraceHelper\ExceptionPrinter; try { // Some code that might throw an exception throw new \RuntimeException("Database connection failed"); } catch (\Throwable $e) { // Get a formatted exception string echo ExceptionPrinter::exception($e); // Or print directly to output ExceptionPrinter::print($e); }
Context-Aware Exceptions
<?php use Tourze\BacktraceHelper\ContextAwareInterface; use Tourze\BacktraceHelper\ContextAwareTrait; // Create your own context-aware exception class BusinessException extends \Exception implements ContextAwareInterface { use ContextAwareTrait; } // Throw with context throw new BusinessException( "User not found", 404, null, [ "user_id" => 123, "action" => "profile_view", "timestamp" => time() ] );
Clean Proxy Class Names
<?php use Tourze\BacktraceHelper\NameCleaner; // Clean AOP proxy class names $clean = NameCleaner::formatClassName('AopProxy\__PM__\App\Service\UserService\Generated27a0b72656b351f2c469b189f98d296a'); // Returns: App\Service\UserService // Clean Doctrine proxy class names $clean = NameCleaner::formatClassName('Proxies\__CG__\AppBundle\Entity\User'); // Returns: AppBundle\Entity\User
Advanced Usage
Custom Log Data
Implement the LogDataInterface to provide custom log data for your objects:
<?php use Tourze\BacktraceHelper\LogDataInterface; class User implements LogDataInterface { private int $id; private string $email; public function toLogData(): ?array { return [ 'id' => $this->id, 'email' => $this->email, 'type' => 'user' ]; } }
Production Environment Optimization
The library automatically detects and filters unnecessary frames in production:
// These frames are automatically filtered: // - Composer autoload internals // - Symfony EventDispatcher internals // - Symfony HttpKernel internals // - Spatie Backtrace internals
Integration with Logging
<?php use Tourze\BacktraceHelper\ExceptionPrinter; use Monolog\Logger; $logger = new Logger('app'); try { // Your application code } catch (\Throwable $e) { $logger->error('Application error', [ 'exception' => ExceptionPrinter::exception($e), 'trace' => Backtrace::create()->toString() ]); }
Testing
Run the test suite:
./vendor/bin/phpunit packages/backtrace-helper/tests
Run static analysis:
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/backtrace-helper
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -am 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Development Standards
- Follow PSR-12 coding standards
- Write unit tests for new features
- Ensure all tests pass before submitting PR
- Keep documentation up to date
License
The MIT License (MIT). Please see License File for more information.
tourze/backtrace-helper 适用场景与选型建议
tourze/backtrace-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 35.74k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 tourze/backtrace-helper 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tourze/backtrace-helper 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 35.74k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 22
- 依赖项目数: 23
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-25