/** * 初始化请求的路由字符串 */ protected static function initRouterUrl() { $os_type = strtoupper(PHP_OS); if (strpos($os_type, 'WIN') !== false) { $arr = Request::request()->get(); self::$router_url = isset($arr['__QP_url']) ? $arr['__QP_url'] : ''; return; } if (strpos($os_type, 'LINUX') !== false) { $uri = Request::request()->getURI(); $pos = strpos($uri, '?'); if ($pos !== false) { $uri = substr($uri, 0, $pos); } $uri = str_replace("\\", "/", $uri); while (true) { $tmp = str_replace("//", "/", $uri); if ($tmp === $uri) { break; } $uri = $tmp; } self::$router_url = $uri; return; } self::$router_url = ""; }
/** * 记录起始请求日志 * 记录成功返回true,失败或没记录日志返回false * * @param \Exception|\Throwable $ex * @return bool */ public static function error_log($ex) { if (!BaseLog::isLog('error')) { return false; } if (!Config::getEnv("app.framework_error_log")) { return false; } $data = Request::nonPostParam(); if (Config::getEnv("app.request_log_post")) { $data = Request::param(); } $log_msg = "\r\nQP->Main最外层捕捉到Exception异常:\r\n请求参数:{Param}\r\n异常信息:{E_Msg}\r\n异常位置:{E_Point}\r\n更多异常队列信息:{E_Trace}\r\n"; $log_data = ['Param' => json_encode($data), 'E_Msg' => $ex->getMessage(), 'E_Point' => $ex->getFile() . ":" . $ex->getLine(), 'E_Trace' => json_encode($ex->getTrace())]; return Log::error($log_msg, $log_data, true, 'framework'); }
/** * 底层日志记录方法 * 记录成功返回true,失败或没记录日志返回false * * @param string $log_level 日志等级 * @param string $msg 日志消息(占位符使用{键名}) * @param array $data 数据(支持key-value形式的数组,用来替换$msg中的占位字符) * @param bool $is_replace 是否替换占位字符 * @param string $modular 应用模块名 * @return bool */ protected static function log($log_level, $msg = '', $data = [], $is_replace = false, $modular = 'unknown_module') { if (!in_array($log_level, self::$log_level_allow)) { throw new \InvalidArgumentException("不支持的日志等级:" . $log_level); } if (!self::isLog($log_level)) { return false; } $file_path = self::handle_log_file($modular, $log_level); $log_time = date('Y-m-d H:i:s'); $ip = \Qp\Kernel\Request::getIp(); $router_url = \Qp\Kernel\Http\Router\QpRouter::getRouterStr(); $prefix = "[{$log_time}] [{$ip}] [router : {$router_url}] "; if ($is_replace) { $msg = self::interpolate($msg, $data); } else { $msg = $msg . json_encode(['data' => $data]); } $logger = new FileAdapter($file_path); $logger->setFormatter(new LineFormatter("%message%")); return (bool) $logger->log($prefix . $msg); }