框架升级后旧的代码报错

riyue

问题描述

刚执行完composer update,核心workerman/webman-framework从1.6.9升级到1.6.14,项目启动后报错,升级前是正常的。
根据日志我能看出来是我自定义的日志处理文件中使用了request()函数导致的,升级后不能这样用了吗?
我需要将日志根据请求创建在不同目录中,现在如何实现呢?

报错信息

D:\webman\WMProject>php windows.php 

Fatal error: Uncaught Error: Typed static property Workerman\Worker::$eventLoopClass must not be accessed before initialization in D:\webman\WMProject\vendor\workerman\webman-framework\src\Context.php:75
Stack trace:
#0 D:\webman\WMProject\vendor\workerman\webman-framework\src\Context.php(63): Webman\Context::getKey()
#1 D:\webman\WMProject\vendor\workerman\webman-framework\src\Context.php(92): Webman\Context::getObject()
#2 D:\webman\WMProject\vendor\workerman\webman-framework\src\App.php(614): Webman\Context::get('Webman\\Http\\Req...')
#3 D:\webman\WMProject\vendor\workerman\webman-framework\src\support\helpers.php(323): Webman\App::request()
#4 D:\webman\WMProject\support\com\my\monolog\MonologHandler.php(44): request()
#5 D:\webman\WMProject\vendor\workerman\webman-framework\src\support\Log.php(93): support\com\my\monolog\MonologHandler->__construct(NULL, 10485760, 100, true, 420)
#6 D:\webman\WMProject\vendor\workerman\webman-framework\src\support\Log.php(77): support\Log::handler('support\\com\\pas...', Array, Array)
#7 D:\webman\WMProject\vendor\workerman\webman-framework\src\support\Log.php(55): support\Log::handlers(Array)
#8 D:\webman\WMProject\config\process.php(23): support\Log::channel()
#9 D:\webman\WMProject\vendor\workerman\webman-framework\src\Config.php(207): include('D:\\webman\\Pasis...')
#10 D:\webman\WMProject\vendor\workerman\webman-framework\src\Config.php(63): Webman\Config::loadFromDir('D:\\webman\\Pasis...', Array)
#11 D:\webman\WMProject\vendor\workerman\webman-framework\src\support\App.php(162): Webman\Config::load('D:\\webman\\Pasis...', Array)
#12 D:\webman\WMProject\windows.php(23): support\App::loadAllConfig(Array)
#13 {main}

截图报错信息里报错文件相关代码

config/log.php

use support\com\my\monolog\MonologHandler;

return [
    // 日志保留天数(0代表永久保留)
    'storage_day' => 180,
    // 默认日志通道
    'default'     => [
        // 处理默认通道的handler,可以设置多个
        'handlers' => [
            [
                // 日志处理类
                'class'       => MonologHandler::class,
                'constructor' => [
                    // 日志通道路径
                    null,
                    // 单个日志文件大小 (默认 10MB) 单位字节
                    10485760,
                    Monolog\Logger::DEBUG, // 日志级别
                    // 日志被当前handler处理后是否接着向基类进行传递
                    true,
                    // 写文件权限
                    0644
                ],
                // 日志格式化类及构造函数参数
                'formatter'   => [
                    'class'       => Monolog\Formatter\LineFormatter::class,
                    'constructor' => [null, 'Y-m-d H:i:s.u', true],
                ],
            ]
        ],
    ],
];

MonologHandler.php是自定义类,构造函数如下

/**
     * @param string $channelDirName 日志通道路径
     * @param int $maxFileSize 单个日志文件大小 (默认 10MB)
     * @param int $level 触发此处理程序的最小日志记录级别
     * @param bool $bubble 日志被当前handler处理后是否接着向基类进行传递
     * @param int|null $filePermission 写文件权限 (默认 (0644) )
     * @param bool $useLocking 写日志时是否锁定文件(默认 false)
     *
     * @throws Exception
     */
    public function __construct($channelDirName = null, $maxFileSize = 10485760, $level = Logger::DEBUG, bool $bubble = true, int $filePermission = null, bool $useLocking = false)
    {
        // 请求对象
        $request = request();
        // 多应用支持
        if ($request && !empty($request->app)) {
            $app = $request->app . DIRECTORY_SEPARATOR;
        } else {
            $app = 'cli' . DIRECTORY_SEPARATOR;
        }
        $this->runtimeLogPath = runtime_path() . DIRECTORY_SEPARATOR . 'logs' . DIRECTORY_SEPARATOR . $app . DIRECTORY_SEPARATOR;
        $this->channelDirName = $channelDirName;
        $dateDir              = date('Ym') . DIRECTORY_SEPARATOR;
        $filename             = date('d') . '.log';
        $fullFilePath         = empty($channelDirName) ? $this->runtimeLogPath . $dateDir . $filename : $this->runtimeLogPath . $this->channelDirName . DIRECTORY_SEPARATOR . $dateDir . $filename;
        $this->maxFileSize    = (int)$maxFileSize;
        if ($maxFileSize <= 0) {
            throw new Exception('单个日志文件大小最小值为0!');
        }
        parent::__construct($fullFilePath, $level, $bubble, $filePermission, $useLocking);
    }

操作系统及workerman/webman等框架组件具体版本

Windows11,PHP8.3,workerman/webman最新版,workerman/webman-framework 1.6.14

121 1 1
1个回答

riyue

等不上回复了,也没时间深入逐行看代码了,在MonologHandler.php类中对$request = request();外层增加了个try{}catch(){},先用着,先应付着用

  • 暂无评论
×
🔝