1
5

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 3 years have passed since last update.

【Laravel】ログインログを指定したファイルに保存する

Posted at

ログインログは出力ファイルを分ける

カスタムチャンネルを追加する
storage/logs/login.logに保存されるようにする

config/logging.php
    'channels' => [
        
        'login' => [
            'driver' => 'single',
            'path' => storage_path('logs/login.log'),
        ],

キャッシュクリア

$ php artisan config:clear

ログイン後にログを保存する

app/Http/Controllers/LoginController.php
    /**
     * 認証を処理する
     *
     * @param  \Illuminate\Http\Request $request
     * @return Response
     */
    public function login(Request $request)
    {
        $credentials = $request->only('id', 'password');
        $remember = $request->all()['remember'] ?? false;

        if (\Auth::guard('users')->attempt($credentials, $remember)) {
            \Log::channel('login')->info('ログメッセージ'); // ここを追加
            return redirect()->intended('/');
        }
        return view('auth.login');
    }

ついでに、ログローテーションを導入する

.envのLOG_CHANNELを変更することでログローテーションの導入が可能

LOG_CHANNEL=stack
 ↓ 以下に変更
LOG_CHANNEL=daily

channelの設定は以下のようになっているので、
dailyにすると14日間でローテーションすることになる。保存先はlaravel.log

config/logging.php
    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['single'],
            'ignore_exceptions' => false,
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
        ],

        'daily' => [
            'driver' => 'daily',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

        'slack' => [
            'driver' => 'slack',
            'url' => env('LOG_SLACK_WEBHOOK_URL'),
            'username' => 'Laravel Log',
            'emoji' => ':boom:',
            'level' => 'critical',
        ],

        'papertrail' => [
            'driver' => 'monolog',
            'level' => 'debug',
            'handler' => SyslogUdpHandler::class,
            'handler_with' => [
                'host' => env('PAPERTRAIL_URL'),
                'port' => env('PAPERTRAIL_PORT'),
            ],
        ],

        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'formatter' => env('LOG_STDERR_FORMATTER'),
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],

        'syslog' => [
            'driver' => 'syslog',
            'level' => 'debug',
        ],

        'errorlog' => [
            'driver' => 'errorlog',
            'level' => 'debug',
        ],

        'null' => [
            'driver' => 'monolog',
            'handler' => NullHandler::class,
        ],

        'emergency' => [
            'path' => storage_path('logs/laravel.log'),
        ],
1
5
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
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?