Can anybody tell me how Can I use debugger on the Cron.
I tried to do many things, but it doesn't work.
Can anybody tell me how Can I use debugger on the Cron.
I tried to do many things, but it doesn't work.
This Might Help You
i expect You have configured xdebug on Your Project
Now Add a New Debug Configurations Like this
Now You can Run the Cli command Like this
XDEBUG_TRIGGER=yes php bin/magento cron:run
I hope this will help You
You can use a logger to debug cron.
If you are using logger for only testing purpose, you can use direct code either use proper magento standard
With Magento 2.4.4 & plus versions use below code:
$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/testing.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info('Test the log');
$logger->info(var_dump($yourVariable));
For Magento 2.4.2 & lower versions:
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/custom.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info('Custom message');
$logger->info(var_dump($yourVariable));
UPDATE:
Try below:
As mentioned by MagePal Extensions in your comment.
Create a new file, for example, test.php, and put it in your magento2 pub directory with the following code:
You have to replace NameSpace\YourClassName
with your cron class
. And then hit this file in browser to debug the code for cron. Your cron will execute by hitting this file on browser.
<?php
require __DIR__ . '/../app/bootstrap.php';
class TestMyClass
extends \Magento\Framework\App\Http
implements \Magento\Framework\AppInterface
{
public function launch()
{
$this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML);
$myClass = $this->_objectManager->create('NameSpace\YourClassName');
$myClass->execute(); // In cron jobs we usually use the execute() method as the entry point
return $this->_response;
}
}
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('TestMyClass');
$bootstrap->run($app);