### 原由 在tp5版本的时候日志中保存了全部的请求信息,保存了请求地址 请求方法 请求路由 请求头 请求参数,但是在tp6中官方取消了。官方解释说由于日志记录了所有的运行错误,因此养成经常查看日志文件的习惯,可以避免和及早发现很多的错误隐患。 为了定位线上问题,我觉得这些信息还是有必要的 ### 方法 需要修改两个地方,1是修改配置文件,指定自定义的驱动。2是自定义驱动的save方法 ### 代码 config/log.php ```php env('log.channel', 'file'), // 日志记录级别 'level' => [], // 日志类型记录的通道 ['error'=>'email',...] 'type_channel' => [], // 关闭全局日志写入 'close' => false, // 全局日志处理 支持闭包 'processor' => null, //ThinkPHP对系统的日志按照级别来分类记录,按照PSR-3日志规范,日志的级别从低到高依次为: debug, info, notice, warning, error, critical, alert, emergency,ThinkPHP额外增加了一个sql日志级别仅用于记录SQL日志(并且仅当开启数据库调试模式有效)。 // 日志通道列表 'channels' => [ 'file' => [ // 日志记录方式 //'type' => 'File', 'type' => '\app\driver\log\Tp6Log', // 日志保存目录 'path' => '', // 单文件日志写入 'single' => false, // 独立日志级别 'apart_level' => ['error'], // 最大日志文件数量 'max_files' => 0, // 使用JSON格式记录 'json' => false, // 文件大小 'file_size' => 1024*1024*2, // 日志处理 'processor' => null, // 关闭通道日志写入 'close' => false, // 日志输出格式化 'format' => '[%s][%s] %s', // 是否实时写入 'realtime_write' => true, ], // 其它日志通道配置 ], ]; ``` app\driver\log\Tp6Log.php ```php 'c', 'single' => false, 'file_size' => 2097152, 'path' => '', 'apart_level' => [], 'max_files' => 0, 'json' => false, 'json_options' => JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES, 'format' => '[%s][%s] %s', ]; protected $app; // 实例化并传入参数 public function __construct(App $app, $config = []) { $this->app = $app; if (is_array($config)) { $this->config = array_merge($this->config, $config); } if (empty($this->config['format'])) { $this->config['format'] = '[%s][%s] %s'; } if (empty($this->config['path'])) { $this->config['path'] = $app->getRuntimePath() . 'log'; } if (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) { $this->config['path'] .= DIRECTORY_SEPARATOR; } } /** * 日志写入接口 * @access public * @param array $log 日志信息 * @return bool */ public function save(array $log): bool { $destination = $this->getMasterLogFile(); $path = dirname($destination); !is_dir($path) && mkdir($path, 0755, true); $info = []; // 日志信息封装 $time = DateTime::createFromFormat('0.u00 U', microtime())->setTimezone(new DateTimeZone(date_default_timezone_get()))->format($this->config['time_format']); $request = Request::instance(); //新增 $requestInfo = [ 'ip' => $request->ip(), 'method' => $request->method(), 'host' => $request->host(), 'uri' => $request->url() ]; if(isset($log['sql'][0]) && strpos('CONNECT',$log['sql'][0])){ } if (!$this->config['json']) { $debugInfo = [ 'param' => '[ PARAM ] ' . var_export($request->param(), true), 'header' => '[ HEADER ] ' . var_export($request->header(), true) ]; foreach ($debugInfo as $row) { array_unshift($info, $row); } // 增加额外的调试信息 $runtime = round(microtime(true) - $this->app->getBeginTime(), 10); $reqs = $runtime > 0 ? number_format(1 / $runtime, 2) : '∞'; $memory_use = number_format((memory_get_usage() - $this->app->getBeginMem()) / 1024, 2); $time_str = '[运行时间:' . number_format($runtime, 6) . 's] [吞吐率:' . $reqs . 'req/s]'; $memory_str = ' [内存消耗:' . $memory_use . 'kb]'; $file_load = ' [文件加载:' . count(get_included_files()) . ']'; array_unshift($info, $time_str . $memory_str . $file_load); array_unshift($info, "---------------------------------------------------------------\r\n[{$time}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}"); } 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'])) { //这一句很关键,可以给mysql或者其他独立的日志,也加上请求和时间等信息 array_unshift($message, "---------------------------------------------------------------\r\n[{$time}] {$requestInfo['ip']} {$requestInfo['method']} {$requestInfo['host']}{$requestInfo['uri']}"); // 独立记录的日志级别 $filename = $this->getApartLevelFile($path, $type); $this->write($message, $filename); continue; } $info[$type] = $message; } if ($info) { return $this->write($info, $destination); } return true; } /** * 获取主日志文件名 * @access public * @return string */ protected function getMasterLogFile(): string { if ($this->config['max_files']) { $files = glob($this->config['path'] . '*.log'); try { if (count($files) > $this->config['max_files']) { unlink($files[0]); } } catch (Exception $e) { // } } if ($this->config['single']) { $name = is_string($this->config['single']) ? $this->config['single'] : 'single'; $destination = $this->config['path'] . $name . '.log'; } else { if ($this->config['max_files']) { $filename = date('Ymd') . '.log'; } else { $filename = date('Ym') . DIRECTORY_SEPARATOR . date('d') . '.log'; } $destination = $this->config['path'] . $filename; } return $destination; } /** * 获取独立日志文件名 * @access public * @param string $path 日志目录 * @param string $type 日志类型 * @return string */ protected function getApartLevelFile(string $path, string $type): string { if ($this->config['single']) { $name = is_string($this->config['single']) ? $this->config['single'] : 'single'; $name .= '_' . $type; } elseif ($this->config['max_files']) { $name = date('Ymd') . '_' . $type; } else { $name = date('d') . '_' . $type; } return $path . DIRECTORY_SEPARATOR . $name . '.log'; } /** * 日志写入 * @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; return error_log($message, 3, $destination); } /** * 检查日志文件大小并自动生成备份文件 * @access protected * @param string $destination 日志文件 * @return void */ protected function checkLogSize(string $destination): void { if (is_file($destination) && floor($this->config['file_size']) <= filesize($destination)) { try { rename($destination, dirname($destination) . DIRECTORY_SEPARATOR . time() . '-' . basename($destination)); } catch (Exception $e) { // } } } } ```