调试 Laravel 任务

32

我试图在Laravel队列上进行调试,但没有成功。我想将输出打印到控制台中,就像您在其他任何地方使用dd()一样。

<?php

namespace App\Jobs;

use App\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class ProcessImage implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $image;

    /**
     * Attempt the job a maximum of twice
     *
     * @var int
     */
    public $tries = 2;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Image $image)
    {

        $this->image = $image;

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // set paths for standard and thumbnail size images

        $image          = public_path("assets" . DIRECTORY_SEPARATOR . $this->image->original);
        $product_id     = $this->image->id;
        $product_path   = public_path("assets" . DIRECTORY_SEPARATOR . "images" . DIRECTORY_SEPARATOR .
            "products" . DIRECTORY_SEPARATOR . $product_id);


        $thumbnail_path = $product_path . DIRECTORY_SEPARATOR . "thumbnail";

        if(!is_dir($product_path))
            mkdir($product_path);

        if(!is_dir($thumbnail_path))
            mkdir($thumbnail_path);

        // Resize and save the standard image

        $standard = \Image::make($image)->resize(450, 450, function($constraint)
        {
            $constraint->aspectRatio();
        })->save($product_path);

        dd($standard);

    }
}

如果您想调试代码,您需要组装命令字符串,以相应地启动artisan,如下所示 - php -dxdebug.remote_autostart=1 artisan queue:work <queue_name>(来源:https://medium.com/@kebing.yu/make-xdebug-work-with-laravel-queue-b5e73e44a36b) - Daniel Protopopov
使用 Laravel Telescope - Rachid Loukili
6个回答

21

1) 尝试使用php artisan queue:restart - 如果你正在运行队列作为守护程序,由于守护程序将代码加载到内存中,因此您需要在每次更改代码时重新启动侦听器。

2) 在队列中var_dump()dd()Log::info()应该能够正常工作。确保逐步进行调试——在作业的开头记录日志,然后在稍微低一点、更低一点等位置记录,并查看您最后得到记录的位置。

3) 检查 storage/logs 下的 laravel.log - 错误应该会被记录下来。


1
似乎已经被记录在storage/logs中,但是我希望在作业运行时有一种输出到控制台的方式,我认为它应该基于console。 - Ian
你是如何运行队列监听器的?使用 php artisan queue:work 命令吗?你传递了哪些参数? - Criss
如果您正在使用 Sail:sail php artisan queue:restart - Jeffrey Nicholson Carré

9
如果你想要使用 PHP Storm 调试 Laravel Job,请编辑 .env 文件:
QUEUE_DRIVER=sync

将断点放在 handle() 函数中。

6
你不需要在控制台上打印它。失败作业的完整描述存储在异常列(表名fail_jobs)中。
enter image description here

尽管我故意让作业失败并进行了数天的调试,但我的矿井仍然是空的,所以这似乎行不通。 - Hashim Aziz

3
我所做的是记录需要查看的信息,例如在您的情况下:
\Log::info('Making new directory');

或者

\Log::info('this is new image: ', [$standard]);

等等。只需打开日志信息,查看代码中断或应该工作但未工作的条件。


1
我尝试过这个,但是没有输出。https://i.imgur.com/QvMwGE6.png - Ian
是的,有的,Muhammad。 - Ian
这不会在日志文件中输出。 - Shah-Kodes
1
请问您能否解释一下为什么这个方法不起作用?@Shah-Kodes - Muhammad Nauman
如果使用同步驱动程序,则对我有用。 请查看queue.php文件中的'default' => env('QUEUE_CONNECTION', 'sync')行。 - Armalong
在 Laravel 8 中,使用默认设置将日志输出到 Laravel 日志文件对我来说很好。 - Hashim Aziz

0

我使用print_r()函数来完成这个任务。


0

我有两种方法:

  1. 使用 Cache 门面和方法 put()。然后,你可以使用 Cache::get($cacheKey); 来获取它。在这里,我可以传递任何数据类型: Collectionsarraysintegerstring 等。

  2. 使用 Log 门面和方法 debug()。第一个参数是消息,第二个参数是上下文,只能是数组类型

第一种方式更容易调试各种数据,但第二种方式更容易获取调试信息。我更喜欢第一种方式 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接