how to customize file name of log in Laravel 8.0?

夙愿已清 提交于 2020-12-15 04:30:12

问题


i was searching solution for custom Log file name. i got a solution that was working in 5.6 version project. but after that means more than 5.6 it is not working. i try to used many solution but not working. every time file name generating laravel.log. but i want to get it like laravel-{vendor_code}-{date}.log

i used solution that code is below.

CustomLogFile.php

<?php

    namespace App\Logging;
    
    use Illuminate\Http\Request;
    use Monolog\Handler\RotatingFileHandler;
    
    class CustomLogFile
    {
        /**
         * Customize the given logger instance.
         *
         * @param  \Illuminate\Log\Logger  $logger
         * @return void
         */
        public function __invoke($logger )
        {
            $code = 'NA';
            $headers = apache_request_headers();
            if( isset( $headers['DBAuth'] ))
            {
                $code =  dnc($headers['DBAuth']) ;
            }
            elseif ( isset($_REQUEST['code']) )
            {
                $code = $_REQUEST['code'];
            }
    
            foreach ($logger->getHandlers() as $handler) {
                if ($handler instanceof RotatingFileHandler) {
                    $sapi = php_sapi_name();
                    $handler->setFilenameFormat("{filename}-$code-{date}", 'Y-m-d');
                }
            }
        }
    }

Config/ logging.php

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

    'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['daily'],
            'ignore_exceptions' => false,
        ],

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

        'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 14,
        ],

here this code is working as my condition in 5.6 v of Laravel but not more than 5.6 v.

i try to inspect the $logger->getHandlers() . then i am getting stream array is null. so can it be reason of this.


回答1:


may be its answer will be like this in env file

LOG_CHANNEL=daily
LOG_LEVEL=debug

Also Change day setting in Config/logging.php

'daily' => [
            'driver' => 'daily',
            'tap' => [App\Logging\CustomLogFile::class],
            'path' => storage_path('logs/laravel.log'),
            'level' => 'debug',
            'days' => 1,  // 14----> 1

because in 6.2 version i was getting that result. so answer here i will confirm soon with 8.* version.



来源:https://stackoverflow.com/questions/64478010/how-to-customize-file-name-of-log-in-laravel-8-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!