laravel8ログの別名出力

laravel

laravel8のログ出力でログの出力先を変更し、別ファイルへログを出力する方法について。ログについての詳細は本家

プロジェクト作成とサンプルクラス作成

まずプロジェクトを作成する。

$ composer create-project laravel/laravel sandbox --prefer-dist
$ cd sandbox

ログ出力確認用のコマンドクラスを生成する。

$ php artisan make:command SampleLogBatch

ログ出力設定の追記

デフォルトのログ設定ファイルを編集する。

$ vi app/config/logging.php

中身は以下。追記部分にコメントあり。

<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [

    /*
    |--------------------------------------------------------------------------
    | Default Log Channel
    |--------------------------------------------------------------------------
    |
    | This option defines the default log channel that gets used when writing
    | messages to the logs. The name specified in this option should match
    | one of the channels defined in the "channels" configuration array.
    |
    */

    'default' => env('LOG_CHANNEL', 'stack'),

    /*
    |--------------------------------------------------------------------------
    | Log Channels
    |--------------------------------------------------------------------------
    |
    | Here you may configure the log channels for your application. Out of
    | the box, Laravel uses the Monolog PHP logging library. This gives
    | you a variety of powerful log handlers / formatters to utilize.
    |
    | Available Drivers: "single", "daily", "slack", "syslog",
    |                    "errorlog", "monolog",
    |                    "custom", "stack"
    |
    */

    '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',
            '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'),
        ],

        // 今回このブロックを追加
        'sample-log'=>[
            'driver'=>'single',
            'level'=>'debug',
            'path'=>storage_path('logs/sample-log.log'),
        ],
    ],
];

実行クラスの編集

生成したコマンドクラスを編集してログを追記する。

$ vi app/Console/Commands/SampleLogBatch.php 
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SampleLogBatch extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'batch:sample-log';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Log Output Sample';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // いろいろなログ種別で出力してみる
        Log::channel('sample-log')->debug('sample log debug');
        Log::channel('sample-log')->info('sample log info');
        Log::channel('sample-log')->notice('sample log notice');
        Log::channel('sample-log')->warning('sample log warning');
        Log::channel('sample-log')->error('sample log error');
        Log::channel('sample-log')->critical('sample log critical');
        Log::channel('sample-log')->alert('sample log alert');
        Log::channel('sample-log')->emergency('sample log emergency');
        return 0;
    }
}

ログ出力の実行

コマンドを実行。

$ php artisan batch:sample-log

実行後にログが出力されているか確認する。

$ ls storage/logs/
laravel.log  sample-log.log

ログの中身も確認する。

$ cat storage/logs/sample-log.log 
[2021-03-07 01:05:07] local.DEBUG: sample log debug  
[2021-03-07 01:05:07] local.INFO: sample log info  
[2021-03-07 01:05:07] local.NOTICE: sample log notice  
[2021-03-07 01:05:07] local.WARNING: sample log warning  
[2021-03-07 01:05:07] local.ERROR: sample log error  
[2021-03-07 01:05:07] local.CRITICAL: sample log critical  
[2021-03-07 01:05:07] local.ALERT: sample log alert  
[2021-03-07 01:05:07] local.EMERGENCY: sample log emergency 

処理によってログの出力を変えるのはよくある事なので、うまく使おう。

コメント

タイトルとURLをコピーしました