6
3

More than 3 years have passed since last update.

laravel ログファイル log 複数分ける

Last updated at Posted at 2019-09-27

laravelのログ・ファイルを日時別に分けず、一つのファイルにする

laravel-2019-11-08 ではなく、
laravel.log という一つのログファイルにする。

logging.php

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

.env


LOG_CHANNEL=single


php artisan config:clear

OK!

credit用 ログファイルを作成

logging.php

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


ログのとり方


Log::channel("credit")->debug("これが記載");

OK

laravel ログの パーミッション変更

laravelのログファイルを見るphpを組んでいるんだが、
毎回削除するようにするとふとした瞬間に権限エラーになる。

ということで、

・logファイルの権限を変更
・ログを表示したらファイルを空にする。

ということをやってみよう。

エラー内容


UnexpectedValueException
The stream or file "/path/to/storage/logs/laravel.log" could not be opened: 
failed to open stream: Permission denied


権限を置き換え


sudo chown -R nginx:nginx storage
sudo chmod -R 775 storage


これで動くようになる。

ログファイル表示して削除はこれで。


//    ログ・ファイル取得し表示
    public function log()
    {
        $log = storage_path()."/logs/laravel.log";

        if(is_file($log)){
            $res = file_get_contents($log);

            $fp = fopen($log, 'r+');
            flock($fp, LOCK_EX);
            ftruncate($fp,0);
            flock($fp, LOCK_UN);
            fclose($fp);

        } else {
            $res = 'ログはありません';
        }


        $res = str_replace(",",",<br>",$res);



        print_r($res);


        die;
    }


行数の取得

Loggingフォルダごと、新規作成。

app\Logging\CustomizeFormatter.php

<?php

namespace App\Logging;

use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;
use Monolog\Processor\WebProcessor;
use Monolog\Processor\ProcessIdProcessor;

class CustomizeFormatter
{
    /**
     * ログのフォーマット
     * @var string
     */
    private $logFormat = '[%datetime% %channel%.%level_name% %extra.class%::%extra.function%(%extra.line%)] %message% %context%' . PHP_EOL;

    /**
     * 日付のフォーマット
     * @var string
     */
    private $dateFormat = 'Y/m/d H:i:s.v';

    /**
     * 渡されたロガーインスタンスのカスタマイズ
     *
     * @param \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($monolog)
    {
        // フォーマットを指定
        $formatter = new LineFormatter($this->logFormat, $this->dateFormat, true, true);

        // extraフィールドの追加
        $ip = new IntrospectionProcessor(Logger::DEBUG, ['Illuminate\\']);

        foreach ($monolog->getHandlers() as $handler) {
            $handler->setFormatter($formatter);
            $handler->pushProcessor($ip);
        }
    }
}

app\Logging\CustomizeFormatter.php

'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'tap' => [App\Logging\CustomizeFormatter::class],//大事
            'level' => 'debug',
        ],

表示される ログ


[2020/03/17 13:44:40.683 local.DEBUG App\Http\Controllers\SuicreditController::webhook(51)] array (
  'id' => 100,

  'name' => 600,

) 

6
3
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
6
3