Mac 下 Laravel monolog 写入日志Permission denied

PHP   2024-09-13 22:26:56   679    
UnexpectedValueException: The stream or file "/Users/www/project/project-name/runtime/logs/2024-09-13.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: 127.0.0.1 GET 0.0.0.0:9520/ UnexpectedValueException: The stream or file "/Users/www/project/project-name/runtime/logs/2024-09-13.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: 保存日志错误:The stream or file "/Users/www/project/project-name/runtime/logs/2024-09-13.log" could not be opened in append mode: Failed to open stream: Permission denied The exception occurred while attempting to log: 「127.0.0.1」不可访问![1726214840-1726214840] in 
/Users/www/project/project-name/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:149 Stack trace: #0 
/Users/www/project/project-name/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(125): Monolog\Handler\StreamHandler->write(Array) #1 
/Users/www/project/project-name/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(48): Monolog\Handler\RotatingFileHandler->write(Array) #2 
/Users/www/project/project-name/vendor/monolog/monolog/src/Monolog/Logger.php(399): Monolog\Handler\AbstractProcessingHandler->handle(Array) #3 /Users/www/project/project-name/vendor/monolog/monolog/src/Monolog/Logger.php(650): Monolog\Logger->addRecord(400, '\xE4\xBF\x9D\xE5\xAD\x98\xE8\xAE\xBF\xE9\x97\xAE\xE6\x97\xA5...', Array) #4 /Users/www/project/project-name/vendor/workerman/framework/src/support/Log.php(137): Monolog\Logger->error('\xE4\xBF\x9D\xE5\xAD\x98\xE8\xAE\xBF\xE9\x97\xAE\xE6\x97\xA5...') #5

记一笔错误
php8.2
mac os 14.x
monolog/monolog ^2.0

看以上错误大致意思为不能打开某个文件,因为权限不足!

 

尝试解决方式
1.通过chmod 命令来修改

sudo chmod -R 777 ~/www/project/project-name/runtime/

发现只生效一次,后面有日志还是会保存权限问题。

看日志后找到 文件vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:149  如下

 /**
     * {@inheritDoc}
     */
    protected function write(array $record): void
    {
        if (!is_resource($this->stream)) {
            $url = $this->url;
            if (null === $url || '' === $url) {
                throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().' . Utils::getRecordMessageForException($record));
            }
            $this->createDir($url);
            $this->errorMessage = null;
            set_error_handler([$this, 'customErrorHandler']);
            try {
                $stream = fopen($url, 'a');
                if ($this->filePermission !== null) {
                    @chmod($url, $this->filePermission);
                }
            } finally {
                restore_error_handler();
            }
            if (!is_resource($stream)) {
                $this->stream = null;

                throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened in append mode: '.$this->errorMessage, $url) . Utils::getRecordMessageForException($record));
            }
            stream_set_chunk_size($stream, $this->streamChunkSize);
            $this->stream = $stream;
        }

        $stream = $this->stream;
        if (!is_resource($stream)) {
            throw new \LogicException('No stream was opened yet' . Utils::getRecordMessageForException($record));
        }

        if ($this->useLocking) {
            // ignoring errors here, there's not much we can do about them
            flock($stream, LOCK_EX);
        }

        $this->streamWrite($stream, $record);

        if ($this->useLocking) {
            flock($stream, LOCK_UN);
        }
    }

解决方法:

在 if($this->filePermission !== null) 上面一行 添加 $this->filePermission=0777,不知道为啥$this->filePermission是等于300,ok,也算是解决了这个问题吧