ThinkPHP默认在生产环境下关闭错误提示,需开启调试模式以查看具体错误(如语法错误、数据库连接失败)。
config/app.php(或.env文件,优先级更高),设置app_debug为true(如.env中添加APP_DEBUG=true)。ThinkPHP内置日志系统,会将错误、异常、SQL执行等信息记录到指定目录,是排查应用层错误的关键。
runtime/log/文件夹下(如runtime/log/202509/15.log)。tail -f /path/to/project/runtime/log/*.log;php think log(显示所有日志),php think log --level=error(仅查看错误日志)。config/log.php中的level设置为debug(记录最详细信息)。ThinkPHP运行在Debian的Nginx/Apache上,服务器日志会记录HTTP请求失败、权限问题、PHP-FPM异常等信息,需结合查看。
/var/log/nginx/error.log;/var/log/apache2/error.log(Debian)或/var/log/httpd/error_log(CentOS)。tail -f /var/log/nginx/error.log(实时监控),grep -i "error" /var/log/nginx/error.log(过滤错误关键词)。pdo_mysql、mbstring、xml),可通过php -m查看已安装扩展,缺失则用sudo apt install php-mysql php-mbstring php-xml安装。runtime/)需赋予Web服务器用户(如www-data)读写权限,执行:sudo chown -R www-data:www-data /path/to/project、sudo chmod -R 755 /path/to/project。require_once 'Lib/Util.php')与实际文件名大小写一致。数据库错误(如SQL语法错误、表不存在、连接失败)是常见错误,可通过开启SQL日志查看执行的SQL语句及错误信息。
config/database.php中设置sql_debug_log为true(记录所有SQL语句),或使用Log::record($sql, 'sql')手动记录。runtime/log/目录下(如runtime/log/sql.log),通过tail -f /path/to/project/runtime/log/sql.log实时查看。ThinkPHP提供多种调试函数和工具,辅助快速定位具体问题:
dump($variable)函数输出变量内容(比var_dump()更友好,会高亮显示),如dump($user);debug_start('label')和debug_end('label')记录代码段的运行时间和内存占用(如debug_start('query_time')…debug_end('query_time'));对于复杂逻辑错误(如代码流程问题、函数调用栈异常),可使用Xdebug扩展进行断点调试(逐步执行代码、查看变量值)。
pecl install xdebug安装,或在php.ini中添加zend_extension=xdebug.so;php.ini中设置xdebug.remote_enable=1(开启远程调试)、xdebug.remote_host=127.0.0.1(IDE所在主机)、xdebug.remote_port=9003(调试端口,默认9003);php.ini中的memory_limit(如memory_limit = 256M)或入口文件index.php中ini_set('memory_limit', '256M');protected $autoWriteTimestamp = false)。app_debug=true);/var/log/php_errors.log或phpinfo()中的error_log路径);runtime目录可写(chmod -R 755 runtime);rewrite规则,Apache需开启mod_rewrite)。