由于www用户和root用户(比如command的cli进程日志)都有可能对log文件进行读写。
如果是由www用户创建的log文件,不会出任何问题。
但是如果是先由root用户创建的log文件,然后再到www用户角色去写,就会出问题了
因为一般默认创建的log文件的权限是 -rw-r--r-
也就是www没有权限去写入root用户创建的log文件。
网上的方法大体就是像下面代码一样在mkdir的时候修改目录的权限
//thinkphp/library/think/log/driver/File.php$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);}
但是上面只能修改文件夹的权限,并没有修改文件夹下具体的.log文件的权限。
【解决办法】:
修改文件:\thinkphp\library\think\log\driver\File.php里的write()函数
protected
function
write(
$message
,
$destination
,
$apart
= false,
$append
= false)
{
...
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 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 */
...
}
二、日志文件忽略导致类不存在问题
define('LOG_PATH', $rootPath . '/log/');
还有一个坑,如果日志文件夹用log,gitinore时经常会忽略log文件夹,此时thinkphp/library/think/log的文件夹也会被忽略,会报错
class not exists:\think\log\driver\File,所以最好办法是不要用log文件夹,改用下面的
define('LOG_PATH', $rootPath . '/Logs/');