1

Can anybody tell me how Can I use debugger on the Cron.

I tried to do many things, but it doesn't work.

3

2 Answers 2

0

This Might Help You

i expect You have configured xdebug on Your Project

Now Add a New Debug Configurations Like this

enter image description here

Now You can Run the Cli command Like this

XDEBUG_TRIGGER=yes php bin/magento cron:run

I hope this will help You

3
  • I configured everything, But When I run this command " XDEBUG_TRIGGER=yes php bin/magento cron:run" Debugger is not working on the Cron
    – Rana Zain
    Sep 22 at 7:06
  • what is schedule for your cron job?
    – Waqar Ali
    Sep 22 at 7:09
  • just set this : * * * * * I am running by the just command to run the cron at any time
    – Rana Zain
    Sep 22 at 7:16
0

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);
4
  • I am using these logs but I want to use debugger on the cron
    – Rana Zain
    Sep 22 at 6:21
  • You should use xdebugger, for that you need to configure xdebug in your Code editor and set enpoints to start debugging. Sep 22 at 6:23
  • I configured and using but xdeugger not working on the crons
    – Rana Zain
    Sep 22 at 6:26
  • @RanaZain I have updated my answer please check that Sep 22 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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