diff --git a/addons/shopro/library/Wechat.php b/addons/shopro/library/Wechat.php index 8efee39..5469f19 100644 --- a/addons/shopro/library/Wechat.php +++ b/addons/shopro/library/Wechat.php @@ -299,11 +299,7 @@ class Wechat extends Model private function setConfig($platform,$appid) { $debug = config('app_debug'); - // open_basedir 下不能写 /tmp,日志改到站点 runtime 目录 - $logPath = defined('RUNTIME_PATH') ? RUNTIME_PATH . 'easywechat' . DIRECTORY_SEPARATOR : ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR . 'easywechat' . DIRECTORY_SEPARATOR; - if (!is_dir($logPath)) { - @mkdir($logPath, 0755, true); - } + $logFile = $this->getWritableLogFile(); $defaultConfig = [ 'log' => [ @@ -312,18 +308,25 @@ class Wechat extends Model // 测试环境 'dev' => [ 'driver' => 'single', - 'path' => $logPath . 'easywechat.log', + 'path' => $logFile, 'level' => 'debug', ], // 生产环境 'prod' => [ 'driver' => 'daily', - 'path' => $logPath . 'easywechat.log', + 'path' => $logFile, 'level' => 'info', ], ], ], ]; + if (!$logFile) { + // 日志目录无一可写时降级为 null 驱动,避免 Monolog 抛异常把登录等接口打成 500 + $defaultConfig['log']['channels'] = [ + 'dev' => ['driver' => 'null'], + 'prod' => ['driver' => 'null'], + ]; + } if($appid){ $Appsmodel = new \app\common\model\Apps; $APP = $Appsmodel->where(['app_id'=>$appid])->find(); @@ -343,4 +346,31 @@ class Wechat extends Model // 根据框架 debug 合并 log 配置 $this->config = array_merge($this->config, $defaultConfig); } + + /** + * 获取可写的 easywechat 日志文件,全部不可写时返回 null + * + * @return string|null + */ + private function getWritableLogFile() + { + $dirs = [ + // open_basedir 下不能写 /tmp,优先写站点 runtime 目录 + (defined('RUNTIME_PATH') ? RUNTIME_PATH : ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR) . 'easywechat' . DIRECTORY_SEPARATOR, + // runtime 不可写时退到 PHP 临时目录(php-fpm 池已配置 sys_temp_dir) + sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'easywechat' . DIRECTORY_SEPARATOR, + ]; + + foreach ($dirs as $dir) { + if (!is_dir($dir)) { + @mkdir($dir, 0755, true); + } + $file = $dir . 'easywechat.log'; + if (is_dir($dir) && is_writable($dir) && (!file_exists($file) || is_writable($file))) { + return $file; + } + } + + return null; + } }