LoginSignup
86
68

Laravel リクエストログを出力する

Last updated at Posted at 2019-06-18

2024/4/10 追記: こちらの記事をライブラリ化しました。

https://qiita.com/ucan-lab/items/dc33ab7df03ebf485898

Laravelで実際に実行されたリクエストログを出力します。

環境

  • PHP 8.0.1
  • Laravel 8.23.1

補足

  • 環境変数でログ出力の有効/無効の切り替えをしたい

RequestLogger を作成

$ php artisan make:middleware RequestLogger

app/Http/Middleware/RequestLogger.php ファイルが生成される。

app/Http/Kernel.php へ追記

    protected $middleware = [
        // ... 省略
        \App\Http\Middleware\RequestLogger::class,
    ];

$middleware プロパティに RequestLogger を登録します。

app/Http/Middleware/RequestLogger.php を編集

<?php declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class RequestLogger
{
    private array $excludes = [
        '_debugbar',
    ];

    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (config('logging.request.enable')) {
            if ($this->isWrite($request)) {
                $this->write($request);
            }
        }

        return $next($request);
    }

    /**
     * @param Request $request
     * @return bool
     */
    private function isWrite(Request $request): bool
    {
        return !in_array($request->path(), $this->excludes, true);
    }

    /**
     * @param Request $request
     */
    private function write(Request $request): void
    {
        Log::debug($request->method(), ['url' => $request->fullUrl(), 'request' => $request->all()]);
    }
}

config/logging.php を編集

config/logging.php
return [
    /*
    |--------------------------------------------------------------------------
    | Custom Log
    |--------------------------------------------------------------------------
    */

    'request' => [
        'enable' => env('LOG_REQUEST_ENABLE', false),
    ],
];

ログ設定ファイルに追加してます。デフォルトは無効にしてます。

.env を編集

.env
LOG_REQUEST_ENABLE=true

環境設定ファイルで有効にしてご利用ください。

お試し実行

routes/api.php

<?php declare(strict_types=1);

Route::get('/', function () {
    return 'welcome get' . PHP_EOL;
});

Route::post('/', function () {
    return 'welcome post' . PHP_EOL;
});

適当に文字列を返すAPIを作成する。

WEBサーバを起動する

$ php artisan serve

Laravelのビルトインサーバを起動する

APIを実行する

$ curl http://127.0.0.1:8000/api?foo=bar&hoge=fuga
$ curl http://127.0.0.1:8000/api -F 'foo=bar' -F 'hoge=fuga'

GET と POST を curl コマンドを使って叩いてみる

storage/logs/laravel-***.log

ログファイルを確認すると次のようなデータベースクエリログが出力されていればok

[2019-06-17 17:31:54] local.DEBUG: GET {"url":"http://127.0.0.1:8000/api?foo=bar","request":{"foo":"bar"}} 
[2019-06-17 17:32:13] local.DEBUG: POST {"url":"http://127.0.0.1:8000/api","request":{"foo":"bar","hoge":"fuga"}} 

関連記事

86
68
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
86
68