spatie/backtrace
Spatie Backtrace provides a cleaner, easier-to-use alternative to PHP’s debug_backtrace. It returns a structured list of Frame objects with accurate file, line, class/method info, and optional arguments, making traces simpler to inspect and filter.
debug_backtrace, exception handlers) by providing a cleaner, more structured API for backtrace analysis. It integrates seamlessly with Laravel’s error handling (e.g., App\Exceptions\Handler) and logging systems (e.g., Monolog).withArguments(), trimFilePaths(), startingFromFrame()) allows selective feature adoption, reducing coupling with core Laravel systems.ArgumentReducer implementations enable tailored argument formatting (e.g., for Laravel-specific types like Carbon, Illuminate\Support\Collection), aligning with Laravel’s dependency injection and service container patterns.debug_backtrace() calls with Spatie\Backtrace\Backtrace::create(), with optional chaining for advanced features.base_path(), artisan CLI detection, and open_basedir constraints reduces edge-case handling.createForThrowable(), enabling enriched error reporting in App\Exceptions\Handler.withArguments()) and objects (withObject()) may impact performance in high-throughput applications (e.g., API endpoints). Mitigation: Use selectively in debug/error contexts.zend.exception_ignore_args setting must be disabled pre-throw to capture arguments in throwables. Risk: Inconsistent behavior if not configured globally.trimFilePaths() relies on accurate applicationPath configuration. Risk: Misconfigured paths may break frame resolution. Mitigation: Validate paths during initialization.debug_backtrace() entirely, or supplement it for specific use cases (e.g., custom error pages, logging)?withArguments()) is justified despite overhead?ArgumentReducer implementations be created for Laravel’s core types (e.g., Carbon, Collection)?render(), report())?Log facade for structured error logging.tinker and artisan debugging with clearer backtrace output.debug_backtrace()
debug_backtrace() calls with Spatie\Backtrace\Backtrace::create()->frames().// Before
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 5);
// After
$trace = Spatie\Backtrace\Backtrace::create()->limit(5)->frames();
App\Exceptions\Handler to use createForThrowable() for exceptions:
public function render($request, Throwable $exception)
{
$frames = Spatie\Backtrace\Backtrace::createForThrowable($exception)
->withArguments()
->applicationPath(base_path())
->trimFilePaths()
->frames();
// Custom logic with $frames
}
ArgumentReducer instances for types like Carbon or Collection:
$backtrace = Spatie\Backtrace\Backtrace::create()
->withArguments()
->reduceArguments(
Spatie\Backtrace\Arguments\ArgumentReducers::default([
new CarbonArgumentReducer(),
])
);
App\Exceptions\Handler and critical error paths.tinker or custom artisan commands.applicationFrame property).withArguments()) is the primary scalability consideration. Disable in production for high-load paths.| Failure Scenario | Impact | Mitigation |
|---|---|---|
applicationPath misconfigured |
Incorrect applicationFrame labels |
Validate path during initialization. |
zend.exception_ignore_args enabled |
Missing arguments in throwables | Document requirement in README. |
open_basedir restrictions |
Frame file access failures | Use try-catch with fallback to raw backtrace. |
| Custom reducer errors | Argument formatting failures | Graceful fallback to default reducers. |
create()->withArguments()->limit(5)).applicationFrame and trimFilePaths() for path handling.How can I help you explore Laravel packages today?