版本:thinkphp5.1

thinkphplibrarythinklogdriverFile.php 148行

问题:最近项目发布到linux系统centos上,Nginx运行的用户是www 而日志 write则变成了root用户 这样www无法读取日志就造成程序错误。linux有严格的权限管理!先根据网上的攻略进行改造!记录如下:

/**
 * 日志写入
 * @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) {
        $info[$type] = is_array($msg) ? implode("\r\n", $msg) : $msg;
    }

    if (PHP_SAPI == 'cli') {
        $message = $this->parseCliLog($info);
    } else {
        // 添加调试日志
        $this->getDebugLog($info, $append, $apart);

        $message = $this->parseLog($info);
    }
    //解决root产生的日志 www无法访问
    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;
}

标签: none