spatie/backtrace
Spatie Backtrace makes PHP backtraces easier than debug_backtrace(): frames are aligned correctly and exposed as Frame objects with file, line, class, method, and optional arguments. Simple API to capture, inspect, and work with stack traces.
debug_backtrace, addressing key pain points (e.g., skewed function names, bitmask complexity) while providing a structured, object-oriented API. This aligns perfectly with Laravel’s debugging needs (e.g., error handling, logging, observability).withArguments(), trimFilePaths(), etc.) allows selective feature adoption, reducing coupling with core systems.applicationPath(base_path()) and CLI-frame detection (e.g., artisan) demonstrate awareness of Laravel’s conventions, enabling seamless integration.applicationPath) make adoption trivial. No database migrations or complex setup required.debug_backtrace and Laravel’s app()/base_path() helpers, ensuring no breaking changes.Throwable::getTrace() (via createForThrowable()) enables use cases like enhanced error pages (e.g., Laravel’s App\Exceptions\Handler) or logging (e.g., Monolog).withArguments()) adds CPU/memory cost. Mitigation: Use selectively (e.g., only in dev/staging) or disable via zend.exception_ignore_args.artisan) may need tuning for custom CLI scripts. Mitigation: Extend applicationPath() or override frame classification logic.ArgumentReducer implementations require type safety and testing. Mitigation: Start with built-in reducers; extend only for critical types (e.g., DateTime).debug_backtrace entirely, or supplement it (e.g., for specific debugging tools)?render() in Handler) or logging (e.g., Monolog handlers)?withArguments() in production? Can it be gated by environment (e.g., app()->isLocal())?debug_backtrace in custom error handlers, middleware, or service containers (e.g., AppServiceProvider boot method).
// Example: Enhanced error handler
public function render($request, Throwable $exception)
{
$backtrace = Spatie\Backtrace\Backtrace::createForThrowable($exception)
->applicationPath(base_path())
->withArguments()
->trimFilePaths();
return response()->json([
'error' => $exception->getMessage(),
'trace' => $backtrace->frames(),
]);
}
ErrorHandler or custom processors to format backtraces consistently.debug_backtrace in a single component (e.g., a custom exception handler) and compare output quality/performance.debug_backtrace() with Backtrace::create()->frames() in a logging middleware.app('backtrace')->create()) to abstract the package, easing future swaps.config('debug.backtrace_enhanced')) to toggle functionality.debug_backtrace usages to leverage Spatie\Backtrace (e.g., in App\Exceptions\Handler).Throwable improvements) and 10+ (for PHP 8.4).debug_backtrace (e.g., laravel-debugbar, spatie/laravel-activitylog). Override their backtrace methods if needed.debug_backtrace in logging and error handling (high ROI, low risk).ArgumentReducer or startingFromFrame.ArgumentReducer implementations) in a BACKTRACE_CUSTOMIZATIONS.md file.Backtrace::create()->withArguments()->trimFilePaths() for local debugging.php artisan backtrace:dump) to generate formatted backtraces for support tickets.DEBUGGING.md section to the project’s docs with examples for common scenarios (e.g., queue job failures, middleware errors).withArguments() in production unless critical (e.g., for high-severity errors).Symfony\Component\Cache\Adapter).startingFromFrame() to isolate application-specific frames in microservices.trimFilePaths() to normalize paths across environments (e.g., Docker vs. bare metal).zend.exception_ignore_args=0 is enabled, arguments may be exposed in production. Mitigation: Set zend.exception_ignore_args=1 globally or per-environment.open_basedir restrictions (fixed in v1.7.4) or custom applicationPath misconfigurations may break frame classification. Mitigation: Validate paths in CI/CD.$this references) may cause errors. Mitigation: Use try-catch around withObject() calls.Backtrace vs. debug_backtrace for key scenarios (e.g., catching a QueryException).ArgumentReducer implementations (e.g., for Carbon instances).startingFromFrame, offset).trimFilePaths reducing noise in logs?").How can I help you explore Laravel packages today?