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?

More than 3 years have passed since last update.

Laravelでログレベルにしたがってログの出力先を変更する

Last updated at Posted at 2021-04-17

はじめに

Laravelでログレベルにしたがってログの出力先を変更したい時の設定について
例えばerror以上はerror.log分けておきたい。みたいな時に使えます

環境

  • PHP 7.4.16
  • Laravel 8.34.0

やり方

config/loggerに以下を記述

'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['error', 'app'],
            'ignore_exceptions' => false,
        ],

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

        'error' => [
            'driver' => 'single',
            'path' => storage_path('logs/error.log'),
            'ignore_exceptions' => false,
            'level' => 'error',
        ],
]

デフォルトのチャンネルがstackになっているので.envファイル中のLOG_CHANNELに関して以下の設定になっていることを確認

LOG_CHANNEL=stack
or
LOG_CHANNEL=

結果

storage 
└── logs
    ├── app.log
    ├── error.log
    └── laravel.log

app.log

[2021-04-17 12:45:46] local.INFO: this is info log  
[2021-04-17 12:45:51] local.ERROR: this is error log

error.log

[2021-04-17 12:45:51] local.ERROR: this is error log  

app.log, error.logそれぞれに想定するログレベル以上のログが出力されることが確認できた

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?