[英]Laravel Task Scheduling not providing output on Failure
在 Laravel 版本 8.5.0 中,計划任務失敗時,它不會在onFailure
掛鈎中為我提供 output
->onFailure(function (Stringable $error) {
\\ To something with output
}
文檔鏈接: https://laravel.com/docs/8.x/scheduling#task-hooks
這是我目前的設置。
kernel.php
use Illuminate\Support\Stringable;
$cronId = 33;
$cronUrls = $healthChecks->getHealthCheckUrls('cron');
$schedule->command(DoSomethingThatThrowsAnError::class)
->when(function () use ($crons, $cronId) {
return $crons->isActive($cronId);
})
->before(function () {
Log::info('cron "do something that throws an error" has started.');
})
->after(function () {
Log::info('cron "do something that throws an error" has finished.');
})
->onFailure(function (Stringable $error) {
Log::warning("cron 'do something that throws an error' has failed. Error: $error");
})
->pingBefore($cronUrls['start'])
->pingOnSuccess($cronUrls['success'])
->pingOnFailure($cronUrls['failure'])
->days([1, 2, 3, 4, 5])
->at('17:00');
DoSomethingThatThrowsAnError.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\OutputStyle;
class DoSomethingThatThrowsAnError extends Command
{
protected $signature = 'cron:do-something-that-throws-an-error';
protected $description = 'banana';
public function handle()
{
throw new \Exception("banana");
}
}
我例外發生的事情
該異常記錄在laravel.log
中,因為(如果我錯了,請糾正我)所有應用程序異常都記錄在那里。 並且異常將記錄在onFailure
掛鈎中,例如"cron 'do something that throws an error' has failed. Error: (error gibberish)"
實際發生了什么
該異常一般只記錄在laravel.log
中。 此外,調用了onFailure
掛鈎,但$error
參數為空。
為什么這是一個問題
我使用多個日志文件,在當前情況下,我必須將laravel.log
中的異常與另一個日志文件中的onFailure
日志相匹配。 這通常很煩人。
問題
如何設置/獲取異常作為onFailure
鈎子的參數?
只需替換throw new \Exception("banana");
與$this->info('banana');
或$this->info($e->getMessage());
在 catch 塊中。
public function handle()
{
try {
// your code
// send output on success
$this->info('Success message');
} catch (Exception $e) {
// send output on failure
$this->info($e->getMessage());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.