# Laravel Log の基本設定&使い方
Laravel は Monolog (opens new window) ライブラリを利用しており、強力なログハンドラを多種に渡りサポートしています。
Laravel Log はファイルに記録するだけでなく、Slack に飛ばしてチーム全体に通知できます。
ログチャネルの変更は env ファイルから変更可能
.env のデフォルト設定
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
本番なら LOG_LEVEL=error に設定すべき
ログの保存場所は storage/logs/laravel.log
# デフォルト設定
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
'default' => env('LOG_CHANNEL', 'stack'),
'deprecations' => env('LOG_DEPRECATIONS_CHANNEL', 'null'),
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
'days' => 14,
],
'Slack' => [
'driver' => 'Slack',
'url' => env('LOG_Slack_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],
'papertrail' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => SyslogUdpHandler::class,
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
],
],
'stderr' => [
'driver' => 'monolog',
'level' => env('LOG_LEVEL', 'debug'),
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => env('LOG_LEVEL', 'debug'),
],
'errorlog' => [
'driver' => 'errorlog',
'level' => env('LOG_LEVEL', 'debug'),
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];
デフォルトで stack チャンネル debug レベルになっています。
# チャンネルドライバ
| 名前 | 説明 |
|---|---|
| custom | 指定ファクトリを呼び出してチャンネルを作成する |
| daily | 日毎にファイルを作成する RotatingFileHandler ベース |
| errorlog | ErrorLogHandler ベース |
| monolog | Monolog がサポートしているハンドラを使用する Monolog ファクトリ |
| null | すべてのログメッセージを破棄する |
| papertrail | SyslogUdpHandler ベース |
| single | 単一のファイルまたはパスベースのロガーチャンネル(StreamHandler) |
| Slack | SlackWebhookHandler ベース |
| stack | 「マルチチャンネル」チャンネルの作成を容易にするラッパー |
| syslog | SyslogHandler ベース |
# ログレベル
| コード | 重大度 | レベル | 説明 |
|---|---|---|---|
| 0 | emergency 緊急 | 高 | システム停止 |
| 1 | alert アラート | 高 | 直ちに解決すべきエラー |
| 2 | critical 致命的 | 高 | 致命的なエラー |
| 3 | error エラー | 高 | ページエラー |
| 4 | warning ワーニング | 中 | |
| 5 | notice 注意 | 低 | |
| 6 | info 情報 | 低 | |
| 7 | debug デバッグ | 低 |
debug のログレベルが一番低く、emergency のログレベルが一番高い
デフォルトログレベルは debug になっています。
Log::debug('debug error');
Log::info('info error');
Log::notice('notice error');
Log::warning('warning error');
Log::error('error error');
Log::critical('critical error');
Log::alert('alert error');
Log::emergency('emergency error');
# ログ監視
コマンド実行して、ログを監視する
tail -f storage/logs/laravel.log
tail とは
tail は指定ファイルの最終行から数行を表示するコマンド
使い方
tail <ファイル名>
オプション
| オプション | フルー | 説明 |
|---|---|---|
-c | --bytes | 出力する文字数を指定 |
-n | --lines | 出力する行数を指定 |
-q | --quiet | ファイル名を表示しない |
-v | --verbose | ファイル名を常に表示 |
-f | --follow | ファイルの追記を監視 |
# エラーログを Slack に連携する手順
- Laravel Slack ドライバ設定
- Slack Incoming WebHoks 設定
- メッセージを投稿するワークスペースで新しい Slack アプリを作成
- 「機能」ページで「Incoming Webhook をアクティブにする」をオンに
- 「新しい Webhook をワークスペースに追加する」をクリック
- アプリの投稿先となるチャンネルを選択して「許可する」をクリック
- Incoming Webhook URL (opens new window) を使用して Slack に投稿
- Slack で取得した Webhook URL を env に設定
LOG_CHANNEL=slack
LOG_LEVEL=error
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxxxxxxx/xxxxxxxxxxxxxxx
デフォルトでは、Slack は critical レベル以上のログのみ受信します。
参考資料