TP6解决线上服务器生成Log权限问题

tp5 解决root生成的文件,www用户没有写权限的问题

场景:在服务器上添加了一个定时删除cache缓存文件的任务,由于在执行之后会在runtime中生成一个文件,如果正好是月初一号就会创建这个月份的文件夹,由于这个自动任务是root用户执行,运行项目写日志是www用户,所以当项目运行再写入日志时会没有权限。

出现报错

PHP Fatal error: Uncaught exception ‘think\exception\ErrorException’ with message ‘error_log(D:\web\xinluchuntian.com\minishop\runtime\log\201702\11.log): failed to open stream: Permission denied’ in D:\web\xinluchuntian.com\minishop\core\library\think\log\driver\File.php:98

解决办法,需要修改两个位置,首先按找到thinkphp/library/log/driver/file.php

我的项目位置在vendor\topthink\framework\src\think\log\driver\File.php
有些项目可能会有think-log等文件,注意看红色部分

当前tp5版本:5.0.15
1. 找到56行(不同tp版本可能会不一样,save方法中)
!is_dir($path) && mkdir($path, 0755, true);
1
修改为

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

public function save(array $log): bool
    {
        $destination = $this->getMasterLogFile();

        $path = dirname($destination);
//        !is_dir($path) && mkdir($path, 0755, true);
        !is_dir($path) && mkdir($path, 0755, true) && chmod($path,0777);
        $info = [];

        // 日志信息封装
        $time = date($this->config['time_format']);

        foreach ($log as $type => $val) {
            $message = [];
            foreach ($val as $msg) {
                if (!is_string($msg)) {
                    $msg = var_export($msg, true);
                }

                $message[] = $this->config['json'] ?
                    json_encode(['time' => $time, 'type' => $type, 'msg' => $msg], $this->config['json_options']) :
                    sprintf($this->config['format'], $time, $type, $msg);
            }

            if (true === $this->config['apart_level'] || in_array($type, $this->config['apart_level'])) {
                // 独立记录的日志级别
                $filename = $this->getApartLevelFile($path, $type);
                $this->write($message, $filename);
                continue;
            }

            $info[$type] = $message;
        }

        if ($info) {
            return $this->write($info, $destination);
        }

        return true;
    }

2.找到128行(不同tp版本可能会不一样,write方法中)
return error_log($message, 3, $destination);

修改为

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;
 


    /**
     * 日志写入
     * @access protected
     * @param array  $message     日志信息
     * @param string $destination 日志文件
     * @return bool
     */
    protected function write(array $message, string $destination): bool
    {
        // 检测日志文件大小,超过配置大小则备份日志文件重新生成
        $this->checkLogSize($destination);

        $info = [];

        foreach ($message as $type => $msg) {
            $info[$type] = is_array($msg) ? implode(PHP_EOL, $msg) : $msg;
        }

        $message = implode(PHP_EOL, $info) . PHP_EOL;

//        return error_log($message, 3, $destination);
        /** 解决root生成的文件,www用户没有写权限的问题 by Werben 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 Werben 20190704 end */
    }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值