- A+
日志,是我们分析代码和调试bug的好助手。在Thinkphp5 中,日志内的记录可以说是十分详细,但是所有的的日志都集中一个文件内,让我们很难准确定位到问题。因此我们尝试将Thinkphp5 的日志按模块来展示,以此帮助我们更好的分析问题。
官方文档:https://www.kancloud.cn/manual/thinkphp5/118126
首先,我们先修改配置文件,/application/config.php, 修改log 配置:
'log' => [ // 日志记录方式,内置 file socket 支持扩展 'type' => 'app\common\tool\FileLog', // 日志保存目录 'path' => LOG_PATH, // 日志记录级别 'level' => [], ],
然后我们在 /application/common/tool/ 下 创建 FileLog.php 类, 复制 library/think/log/driver/File.php 的内容,修改如下的代码即可:
/** * 获取主日志文件名 * @access public * @return string */ protected function getMasterLogFile() { if ($this->config['single']) { $name = is_string($this->config['single']) ? $this->config['single'] : 'single'; $destination = $this->config['path'] . $name . '.log'; } else { $cli = PHP_SAPI == 'cli' ? '_cli' : ''; if ($this->config['max_files']) { $filename = date('Ymd') . $cli . '.log'; $files = glob($this->config['path'] . '*.log'); try { if (count($files) > $this->config['max_files']) { unlink($files[0]); } } catch (\Exception $e) { } } else { $filename = Request::instance()->module().DIRECTORY_SEPARATOR. date('Ym') . DIRECTORY_SEPARATOR . date('d') . $cli . '.log'; } $destination = $this->config['path'] . $filename; } return $destination; }
修改完成以后, 允许项目,访问任意文件,即可在 runtime/log 目录下看到:
如需调整成其他格式,可以在此文件中修改即可。