出现错误:error_log(/www/wwwroot/******/runtime/log/202407/12.log): Failed to open stream: Permission denied
解决办法:需要修改两个位置,首先按找到thinkphp/library/log/driver/file.php
我的项目位置在vendor\topthink\framework\src\think\log\driver\File.php
有些项目可能会有think-log等文件,注意:是framework 目录
1.找到56行(不同tp版本可能会不一样,save方法中) !is_dir($path) && mkdir($path, 0755, true);
找到后修改这个方法。
/**
* 日志写入接口
* @access public
* @param array $log 日志信息
* @return bool
*/
public function save(array $log): bool
{
$destination = $this->getMasterLogFile();
$path = dirname($destination);
//!is_dir($path) && mkdir($path, 0777, true);
!is_dir($path) && mkdir($path, 0777, true) && chmod($path,0777);
$info = [];
// 日志信息封装
$time = \DateTime::createFromFormat('0.u00 U', microtime())->setTimezone(new \DateTimeZone(date_default_timezone_get()))->format($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);
/**
* 日志写入
* @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;
//这个是修改之后的
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;
//这段是之前的
//return error_log($message, 3, $destination);
}