How to use Debug Backtrace in magento 2?

You can use debug_backtrace() as I added below.

$debugBackTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($debugBackTrace as $item) {
    echo @$item['class'] . @$item['type'] . @$item['function'] . "\n";
}

For reference please check dev\tests\api-functional\framework\Magento\TestFramework\TestCase\Webapi\Adapter\Rest\DocumentationGenerator.php


In the logger classes of Magento 2, the debug_backtrace method is not used directly.

So the Magento 2 way of doing backtrace is to use the Magento\Framework\Debug class (which is the equivalent of the M1 Varien_Debug class) and call the backtrace() method:

/**
 * Prints or returns a backtrace
 *
 * @param bool $return      return or print
 * @param bool $html        output in HTML format
 * @param bool $withArgs    add short arguments of methods
 * @return string|bool
 */
public static function backtrace($return = false, $html = true, $withArgs = true)
{
    $trace = debug_backtrace();
    return self::trace($trace, $return, $html, $withArgs);
}

In any PHP application you can just do:

$e = new \Exception();
echo '<pre>';
print_r($e->getTraceAsString()); 
exit;

Due to name spacing in M2, you need to use new \Exception(); instead of just new Exception();