Laravel 5.5 升级到 5.6

升级composer.json中组件

升级fideloper/proxy

# 修改版本
"fideloper/proxy": "~4.0"
composer update fideloper/proxy

remove其他一些冲突包

这里我们只用了一个php-ffmpeg/php-ffmpeg是冲突的...所以 我要先remove他

composer remove php-ffmpeg/php-ffmpeg

升级其他组件

"php": ">=7.1.3"
"laravel/framework": "5.6.*"
"phpunit/phpunit": "~7.0"
composer update

修改 TrustPrixies

如果不修改 会报错 Argument 2 passed to Symfony\Component\HttpFoundation\Request::setTrustedProxies() must be of the type integer, array given

Http/Middleware/TrustPrixies

protected $headers =  Request::HEADER_X_FORWARDED_ALL;

重装remove的包

这一步很重要..你不但要重装... 还要测试包的兼容性

composer require php-ffmpeg/php-ffmpeg

相信走了以上的步骤... 你的框架已经是5.6的了

修改日志类

5.5和5.6最大的一个区别..就是日志系统做了很大的修改,所以我要修改我们之前logstashFormatter的那个类 否则ELK就会解析不出来日志

复制logging.php到config目录下可以从这里下载

增加

因为我们用的是自定义的日志格式,所以这里我选择新增一个channel

//自定义支持ELK的日志
'logstashlog'=>[
    'driver' => 'daily',
    'path' => storage_path('logs/laravel.log'),
    'tap' => [\App\Foundation\Log\ELKFormatter::class],
    'level' => 'debug',
    'days' => 30
]

修改框架默认使用频道

'stack' => [
        'driver' => 'stack',
        'channels' => ['logstashlog'],
    ],

ELKFormatter类

namespace App\Foundation\Log;

class ELKFormatter
{
    public function __invoke($logger)
    {
        foreach ($logger->getHandlers() as $handler) {
            $handler->setFormatter(new LogstashFormatter(config('settings.Log_stash_name')));
        }
    }
}

LogstashFormatter类

这个类可以用框架的. 因为我们ELK需要增加一个字段来做索引 ...这里只列出需改部分

protected function formatV0(array $record)
{
        //.....在最后新增如下方法
        if (!empty($record['context'])) {
            foreach ($record['context'] as $key => $val) {
                if($key != self::CUSTOM_LOG_TYPE){
                    $message['@fields'][$this->contextPrefix . $key] = $val;
                }else{
                    //获取module_name
                    $message['@custom_log_type'] = $val;
                }
            }
        }
        return $message;
}

修改自定义 LogHandle 类

这个类是我们自定义的.. 是为了方便记录业务日志的.. 方便记录到指定文件夹目录

这个类 写的也是相当的糙.. 😆. 😆. 😆. 😆. 😆

namespace App\Handles;

use Log;
use Monolog\Formatter\LogstashFormatter;
use Monolog\Handler\RotatingFileHandler;
use ReflectionClass;

class LogHandle
{
    protected $path = null;
    protected $name = null;
    protected $suffix = null;
    protected $logClient = null;
    protected $monolog = null;
    protected $laravelHandle = null;

    private function getPath()
    {
        return $this->path ? $this->path : storage_path('logs');
    }

    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

    private function getName()
    {
        return $this->name ? $this->name : date('Y-m-d');
    }

    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    private function getSuffix()
    {
        return $this->suffix ? $this->suffix : 'log';
    }

    public function setSuffix($suffix)
    {
        $this->suffix = $suffix;

        return $this;
    }

    private function init()
    {
        $this->monolog = app('log');
        $this->laravelHandle = $this->monolog->popHandler();
        $dir = $this->getPath();
        if (!is_dir($this->getPath())) {
            mkdir($dir, 0777, true);
        }
        $path = $dir . '/' . $this->getName() . '.' . $this->getSuffix();
        $this->monolog->pushHandler(
            $handler = new RotatingFileHandler($path)
        );
        $handler->setFormatter(new LogstashFormatter(config('settings.Log_stash_name')));

        return $this;
    }

    public function emergency($message)
    {
        $this->init();
        Log::emergency($message);
    }

    public function alert($message)
    {
        $this->init();
        Log::alert($message);
    }

    public function critical($message)
    {
        $this->init();
        Log::critical($message);
    }

    public function error($message)
    {
        $this->init();
        Log::error($message);
    }

    public function warning($message)
    {
        $this->init();
        Log::warning($message);
    }

    public function notice($message)
    {
        $this->init();
        Log::notice($message);
    }

    public function info($message)
    {
        $this->init();
        Log::info($message);
    }

    public function debug($message)
    {
        $this->init();
        Log::debug($message);
    }

    public function __destruct()
    {
        if ($this->monolog) {
            $this->monolog->popHandler();
            $this->monolog->pushHandler($this->laravelHandle);
        }
    }
}

LogHandle使用方法

(new LogHandle())->setPath(storage_path('logs/message'))->setName('message')->info('this is message');

切记... 一定要切断项目的所有相关的服务.. 才能升级 切记..切记..切记