25

In magento 1.x we can use backtrace like

echo Varien_Debug::backtrace(true, true); exit;

How can we use this facility in Magento 2?

7 Answers 7

30

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

3
  • 1
    I edited your answer. There was a piece of incorrect syntax, also I changed it to have better output (I used @ to ignore warnings, for instance when 'class' doesn't exist)
    – 7ochem
    Commented Jan 3, 2017 at 11:45
  • 2
    @krishnaijjadaati95Dev thanks for the answer it works for me Commented Jan 3, 2017 at 12:26
  • Does not work in vendor/magento/framework/App/Http.php in the method launch
    – Black
    Commented Dec 20, 2022 at 9:43
20

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);
}
2
  • 5
    This should be the accepted answer.
    – mpchadwick
    Commented Apr 19, 2017 at 16:10
  • 1
    Please add a usage example.
    – Black
    Commented Dec 20, 2022 at 9:46
11

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();

4
  • thanks for the answer i tried that but it says class Exception' not found in path of my calling class Commented Jan 3, 2017 at 11:29
  • @AshishMadankar - see edit!
    – Paras Sood
    Commented Jan 3, 2017 at 11:35
  • 3
    Or shorter: print_r((new \Exception())->getTraceAsString()); (since PHP 5.4, so safe to use in M2)
    – 7ochem
    Commented Jan 3, 2017 at 11:39
  • 1
    @ParasSood its also working Commented Jan 3, 2017 at 12:26
3

This could also work:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$debugBackTrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($debugBackTrace as $item) {
    $logger->info(@$item['class'] . @$item['type'] . @$item['function']);
}

If you want to see all of the classes and methods called, in a custom log file .

1

You can use PHP function debug_backtrace to debugging in Magento.

Use following code in magento to track the issue by using debug_backtrace

foreach (debug_backtrace() as $_stack) {
    echo ($_stack["file"] ? $_stack["file"] : '') . ':' .
        ($_stack["line"] ? $_stack["line"] : '') . ' - ' .
        ($_stack["function"] ? $_stack["function"] : '').'<br/><hr/>';
 }
exit();

You will see the debug backtrace that will allow you to define the issue source and you will get an idea how to fix the issue.

1

Try this:

\Magento\Framework\Debug::backtrace(false, true, false);
exit();

Or use this if you want to get the response in a logfile:

$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/zend_debug.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);

$response = \Magento\Framework\Debug::backtrace(true, false, false);
$logger->info($response);
0

you can use the native Magento backtrace function from the \Magento\Framework\Debug class and call it whenever you need:

\Magento\Framework\Debug::backtrace(false, true, false);

As a result, you will get this nice HTML debug-backtrace: enter image description here

If you don't like the look of the default Magento backtrace, you can use a custom function and Just copy this code into your PHP class temporarily:

public static function debugHtmlBacktrace($title = '') {
     $result = '';
     foreach(debug_backtrace() as $_stack) {
         $result .= '<tr>
             <td>' . (isset($_stack['file']) ? $_stack['file'] : '') . '</td>
             <td>' . (isset($_stack['line']) ? $_stack['line'] : '') . '</td>
             <td>' . (isset($_stack['function']) ? $_stack['function'] : '') . '</td>
         </tr>';
    }

    return '<br/>' . $title . '<table border="1">
         <tr><td>File</td><td>Line</td><td>Function</td></tr>' . $result . '</table>';
 }

and execute it by:

echo self::debugHtmlBacktrace();

Result:- enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.