温馨提示×

thinkphp在ubuntu上的错误日志怎么查看

小樊
44
2025-11-15 00:31:16
栏目: 编程语言

查看 ThinkPHP 在 Ubuntu 的错误日志

一 查看应用日志 ThinkPHP 自带日志

  • 默认路径为项目根目录下的 runtime/log/,按日期分目录与文件,例如 runtime/log/2025-09/01.log。可直接用命令行实时查看:
    • 实时跟踪当天日志:tail -f runtime/log/$(date +%Y-%m)/$(date +%d).log
    • 查看最近100行:tail -n 100 runtime/log/2025-09/01.log
  • 若使用框架命令查看:在项目根目录执行 php think log(部分版本/安装方式支持)。
  • 版本差异提示:
    • ThinkPHP 5 常见路径为 Application/Runtime/…(老版本结构)。
    • ThinkPHP 6/8 常见路径为 runtime/log/(默认按天分割)。
  • 如未产生日志,检查目录权限,确保 runtime/logwww-data(或运行 PHP 的用户)可写。

二 开启更详细的错误输出便于定位

  • 开启应用调试模式:在 app/config/app.php 或入口文件 public/index.php 中设置
    • ‘app_debug’ => true(或 define(‘APP_DEBUG’, true);)。注意生产环境请勿长期开启。
  • 调整 PHP 错误报告(开发环境):在 php.ini 或入口临时设置
    • ini_set(‘display_errors’, ‘On’);
    • ini_set(‘error_reporting’, E_ALL);
  • 框架日志级别与 SQL 日志:
    • config/log.php 配置 level 为 [‘error’,‘warning’,‘info’,‘debug’] 等以输出更详细日志。
    • 数据库 SQL 日志由 config/database.phptrigger_sql 控制,开发时可设为 true(受 APP_DEBUG 影响)。

三 Web 服务器与 PHP-FPM 日志

  • Nginx:查看 /var/log/nginx/error.log;PHP 错误通常记录在 /var/log/php7.4-fpm.log(版本号可能不同,如 php8.1-fpm.log)。
  • Apache:查看 /var/log/apache2/error.log
  • 配合实时跟踪:tail -f /var/log/nginx/error.log 或 tail -f /var/log/php*.log,可快速定位路由、重写、权限、FPM 进程等问题。

四 常见排查清单

  • 目录与权限:确保 runtime/ 及子目录(尤其是 runtime/log)对 www-data 可写,否则框架无法写日志。
  • 日志配置生效:确认 config/log.phpdefault/channels/file 配置正确,level 未过滤掉 error/debug,必要时开启 realtime_write 便于即时落盘。
  • 入口与服务器配置:核对 public/index.php 入口是否加载到正确应用目录;检查 Nginx/Apacheroot 指向、rewrite 规则、fastcgi_passsocket 路径是否正确。

0