4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Laravel5] キューされたジョブの実行完了のログ

Posted at

初投稿です。

通常のLaravelのログファイルとは別に、キューされたジョブの実行完了時に
月単位でのログを取りたかったので、以下のようにしてみました。

config/app.php
    'queue_logs' => [
        'dir_name'  => 'queue_logs', // ジョブ実行完了のログ出力ディレクトリ名
        'file_name' => 'queue-laravel',   // ジョブ実行完了のログ出力ファイル名
    ],
app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Queue;
use Storage;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    /**
     * アプリケーションサービスの初期化処理
     *
     * @return void
     */
    public function boot ()
    {
        // ジョブ実行完了のログ
        Queue::after(function ($connection, $job, $data) {

            $command = unserialize($data['data']['command']);

            // ログを出力するディスク情報取得
            $disk = Storage::disk('logs');
            $dir_name = config('app.queue_logs.dir_name', 'queue_logs');
            $file_name = config('app.queue_logs.file_name', 'queue-laravel')."-".date('Y-m').".log";

            // ログ出力
            $message = "[".date('Y-m-d H:i:s')."] [".get_class($command)."] complete.";
            $disk->append($dir_name."/".$file_name, $message);

        });
    }

```storage/logs/queue_logs/queue-laravel-2016-06.log`
[2016-06-23 11:54:18] [App\Jobs\sendReminderEmail] complete.



> 参考
> [キュー 5.1 Laravel ジョブイベント](http://readouble.com/laravel/5/1/ja/queues.html#job-events)
4
6
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
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?