#2059·monolog

Consider capturing error stack traces by default in ErrorHandler

Author: SeldaekCreated Aug 28, 2026Updated Sep 1, 2026
LabelsFeature

ErrorHandler::captureStackTraces() was added in 3.11 (see #1448) to attach an \ErrorException carrying the stack trace of the error to context['exception'], so PHP warnings/notices can be traced back to their caller. It is opt-in in 3.x to avoid changing behaviour in a minor release.

For 4.0 we should consider enabling it by default: context['exception'] is the PSR-3 convention, and the absence of a trace is what made #1448 a problem in the first place.

Things to weigh before flipping the default:

  • Handlers which treat context['exception'] specially would start seeing every notice and deprecation as an exception: NewRelicHandler calls newrelic_notice_error() and RollbarHandler reports an exception occurrence (both affect error inventory and quota), PHPConsoleHandler calls dispatchException(), and GelfMessageFormatter derives its file/line additional fields from the exception.
  • NormalizerFormatter::$maxTraceLength defaults to null, so every record would carry a full stack trace. Capping it might be worth doing at the same time.
  • Legacy code bases can emit a lot of E_DEPRECATED, where a full trace per record is rarely worth the volume. E_ALL & ~E_DEPRECATED & ~E_USER_DEPRECATED may be a better default than E_ALL.

The cost itself is not a concern: roughly 1µs and one small object per handled error, and debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) means no arguments are retained.