0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravelのスケジュールでのログの挙動

Last updated at Posted at 2025-01-24

Laravelのスケジュールでコマンドを実行するときは下記の様に実行される。

'/usr/local/bin/php' 'artisan' test:command > '/dev/null' 2>&1

上記は標準出力、標準エラー出力を/dev/nullに出力している。つまりスケジュールで実行した処理の中でprintechoを使い出力した文字列は/dev/nullに消え去ってしまうという事。

普通にログなんかは書き込めます。ただ、問題はstderr等にログで出力するとき。
例えば、dockerのログへ出力したいとき

その場合は

Schedule::command("test:command")
    ->everyMinute()
    ->appendOutputTo('/proc/1/fd/1'); // sendOutputTo('/proc/1/fd/1', ture) でも同じ。

こんな感じでスケジュールをセット。そうすると、

'/usr/local/bin/php' 'artisan' test:command >> '/proc/1/fd/1' 2>&1

こんな感じで実行される。ただ、これだと全てのスケジュールイベントにappendOutputTo('/proc/1/fd/1');をつけないといけないのでスケジュールのFacadeを差し替えると楽。

下記の様なクラスを作って

<?php

namespace App\Scheduling;
use Illuminate\Console\Scheduling\Schedule;

class DockerLog extends Schedule {
    public function exec($command, array $parameters = [])
    {
        return parent::exec($command, $parameters)->appendOutputTo('/proc/1/fd/1');
    }
}

App\Provider\AppServiceProviderregisterメソッドに下記を追加

       $this->app->singleton(Schedule::class, function ($app) {
           $config = $this->app['config'];
           $timezone = $config->get('app.schedule_timezone', $config->get('app.timezone'));
           return new DockerLog($timezone);
       });
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?