ログ

webmanは monolog/monolog を使用してログを処理します。

使用法

<?php
namespace app\controller;

use support\Request;
use support\Log;

class FooController
{
    public function index(Request $request)
    {
        Log::info('log test');
        return response('hello index');
    }
}

提供されるメソッド

Log::log($level, $message, array $context = [])
Log::debug($message, array $context = [])
Log::info($message, array $context = [])
Log::notice($message, array $context = [])
Log::warning($message, array $context = [])
Log::error($message, array $context = [])
Log::critical($message, array $context = [])
Log::alert($message, array $context = [])
Log::emergency($message, array $context = [])

これは以下と同等です

$log = Log::channel('default');
$log->log($level, $message, array $context = [])
$log->debug($message, array $context = [])
$log->info($message, array $context = [])
$log->notice($message, array $context = [])
$log->warning($message, array $context = [])
$log->error($message, array $context = [])
$log->critical($message, array $context = [])
$log->alert($message, array $context = [])
$log->emergency($message, array $context = [])

設定

return [
    // デフォルトのログチャンネル
    'default' => [
        // デフォルトチャンネルのhandlerを処理します。複数設定可能
        'handlers' => [
            [   
                // handlerクラスの名前
                'class' => Monolog\Handler\RotatingFileHandler::class,
                // handlerクラスのコンストラクタ引数
                'constructor' => [
                    runtime_path() . '/logs/webman.log',
                    Monolog\Logger::DEBUG,
                ],
                // フォーマット関連
                'formatter' => [
                    // フォーマット処理クラスの名前
                    'class' => Monolog\Formatter\LineFormatter::class,
                    // フォーマット処理クラスのコンストラクタ引数
                    'constructor' => [ null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
];

複数チャンネル

monologは複数のチャンネルをサポートしており、デフォルトでdefaultチャンネルが使用されます。log2チャンネルを追加する場合、設定は以下のようになります:

return [
    // デフォルトのログチャンネル
    'default' => [
        // デフォルトチャンネルのhandlerを処理します。複数設定可能
        'handlers' => [
            [   
                // handlerクラスの名前
                'class' => Monolog\Handler\RotatingFileHandler::class,
                // handlerクラスのコンストラクタ引数
                'constructor' => [
                    runtime_path() . '/logs/webman.log',
                    Monolog\Logger::DEBUG,
                ],
                // フォーマット関連
                'formatter' => [
                    // フォーマット処理クラスの名前
                    'class' => Monolog\Formatter\LineFormatter::class,
                    // フォーマット処理クラスのコンストラクタ引数
                    'constructor' => [ null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
    // log2チャンネル
    'log2' => [
        // デフォルトチャンネルのhandlerを処理します。複数設定可能
        'handlers' => [
            [   
                // handlerクラスの名前
                'class' => Monolog\Handler\RotatingFileHandler::class,
                // handlerクラスのコンストラクタ引数
                'constructor' => [
                    runtime_path() . '/logs/log2.log',
                    Monolog\Logger::DEBUG,
                ],
                // フォーマット関連
                'formatter' => [
                    // フォーマット処理クラスの名前
                    'class' => Monolog\Formatter\LineFormatter::class,
                    // フォーマット処理クラスのコンストラクタ引数
                    'constructor' => [ null, 'Y-m-d H:i:s', true],
                ],
            ]
        ],
    ],
];

log2チャンネルを使用する場合の使い方は以下の通りです:

<?php
namespace app\controller;

use support\Request;
use support\Log;

class FooController
{
    public function index(Request $request)
    {
        $log = Log::channel('log2');
        $log->info('log2 test');
        return response('hello index');
    }
}