spatie/backtrace
Spatie's PHP package offers a cleaner alternative to debug_backtrace for working with backtraces. Create a Backtrace instance, get an array of Frame objects, and easily read each frame's file, line number, class, method, arguments, and object.
debug_backtrace()'s awkward bitmask options and off-by-one frame attribution with an explicit builder API (Backtrace::create()->frames()), typed Frame objects, and named properties (file, lineNumber, class, method, arguments, object, applicationFrame). Adopting it converts a multi-week internal utility into a composer require.applicationPath(base_path()) and applicationFrame flag mean you can ship a feature that highlights the first application-owned frame — the one a developer actually needs — instead of a wall of Composer internals.withArguments() is opt-in for performance and safety reasons, and reduceArguments() with pluggable ArgumentReducer implementations lets you define exactly how objects (e.g., DateTimeInterface) get stringified before leaving the process. This is a concrete lever for GDPR/compliance-driven roadmap items: you can ship diagnostic context without shipping raw request objects into a log aggregator.limit(), offset(), startingFromFrame(), trimFilePaths(), and code-snippet access (getSnippetAsString) are directly usable for a "copy this trace into a ticket" button, a CLI --debug flag, or a sanitized trace block in an error email — small, shippable features with visible support-cost reduction.Consider adopting when:
artisan and Statamic's please CLI entrypoints are treated as vendor files; the top frame is correctly classified).json_encode.Backtrace::createForThrowable($e)), accepting the documented caveat that arguments depend on zend.exception_ignore_args=0 being set before the throw and that objects are never available on that path.Look elsewhere when:
composer why spatie/backtrace before adding a direct dependency, or you may be formalizing something you already ship.For executives (one paragraph): We have a recurring class of cost — engineers and support spend time reconstructing where a failure happened from raw PHP stack traces that are noisy, misleading, and often expose internal objects. spatie/backtrace is a small, MIT-licensed, actively maintained utility that turns those traces into clean, structured, privacy-safe data with one line of configuration. It is a low-risk enabler for error-page quality, support-triage speed, and PII-safe logging, and it costs us a Composer dependency rather than a build cycle. It is not a monitoring product — it is the formatting layer that makes our existing error handling and error backends actually usable.
For engineering (the pitch): Stop writing backtrace parsers. Backtrace::create()->applicationPath(base_path())->frames() gives you Frame objects with correct class/method attribution (PHP's native version skews the function name to the previous frame), an applicationFrame boolean that already knows about artisan/please edge cases, opt-in withArguments()/withObject() for performance, and a reducer system for safely stringifying objects before they hit a log. Use limit/offset/startingFromFrame to carve out the slice you care about, trimFilePaths to keep paths relative in output, and createForThrowable when you're starting from an exception. It is a thin, well-tested wrapper — no framework lock-in, no runtime service, PHP 8.4 ready.
Risks to state up front: thin abstraction over a native PHP primitive (so the value is ergonomics plus edge-case handling, not novel capability); small maintainer surface; likely already present transitively via Ignition/Flare, so confirm before adding it directly. Mitigation: it is MIT, dependency-light, and trivially replaceable — the cost of being wrong is a one-file swap.
How can I help you explore Laravel packages today?