接口测试正常但线上返回500

如题所述

第1个回答  2022-06-25
1.BOM头

2.runtime生成的文件权限角色与apache的角色不同

runtime的角色为root

apache的角色为www

解决方法:

进入 //thinkphp/library/think/log/driver/File.php

/**

    * 日志写入接口

    * @access public

    * @param  array    $log    日志信息

    * @param  bool    $append 是否追加请求信息

    * @return bool

    */

    public function save(array $log = [], $append = false)

    {

        // $destination = $this->getMasterLogFile();

        // $path = dirname($destination);

        // !is_dir($path) && mkdir($path, 0777, true);

        /* 日志权限修改 */

        $destination = $this->getMasterLogFile();

        $path = dirname($destination);

        if (PHP_SAPI != 'cli') {

            !is_dir($path) && mkdir($path, 0755, true);

        } else {

            !is_dir($path) && mkdir($path, 0777, true) && chmod($path, 0777);

        }

        /* 日志权限修改 */

        $info = [];

        foreach ($log as $type => $val) {

            foreach ($val as $msg) {

                if (!is_string($msg)) {

                    $msg = var_export($msg, true);

                }

                $info[$type][] = $this->config['json'] ? $msg : '[ ' . $type . ' ] ' . $msg;

            }

            if (!$this->config['json'] && (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level']))) {

                // 独立记录的日志级别

                $filename = $this->getApartLevelFile($path, $type);

                $this->write($info[$type], $filename, true, $append);

                unset($info[$type]);

            }

        }

        if ($info) {

            return $this->write($info, $destination, false, $append);

        }

        return true;

    }

/**

    * 日志写入

    * @access protected

    * @param  array    $message 日志信息

    * @param  string    $destination 日志文件

    * @param  bool      $apart 是否独立文件写入

    * @param  bool      $append 是否追加请求信息

    * @return bool

    */

    protected function write($message, $destination, $apart = false, $append = false)

    {

        // 检测日志文件大小,超过配置大小则备份日志文件重新生成

        $this->checkLogSize($destination);

        // 日志信息封装

        $info['timestamp'] = date($this->config['time_format']);

        foreach ($message as $type => $msg) {

            $msg = is_array($msg) ? implode(PHP_EOL, $msg) : $msg;

            if (PHP_SAPI == 'cli') {

                $info['msg']  = $msg;

                $info['type'] = $type;

            } else {

                $info[$type] = $msg;

            }

        }

        if (PHP_SAPI == 'cli') {

            $message = $this->parseCliLog($info);

        } else {

            // 添加调试日志

            $this->getDebugLog($info, $append, $apart);

            $message = $this->parseLog($info);

        }

        //return error_log($message, 3, $destination);

        /** 解决root生成的文件,www用户没有写权限的问题 by WbHuang 20190704 begin */

        if (!is_file($destination)) {

            $first = true;

        }

        $ret = error_log($message, 3, $destination);

        try {

            if (isset($first) && is_file($destination)) {

                chmod($destination, 0777);

                unset($first);

            }

        } catch (\Exception $e) {

        }

        return $ret;

        /** 解决root生成的文件,www用户没有写权限的问题 by WbHuang 20190704 end */

    }