在Ubuntu中管理ThinkPHP日志,可通过以下方式操作:
日志配置
config/log.php 文件,设置日志驱动(如 file、database)、存储路径(默认 runtime/log/)、日志级别(debug/info/error 等)及保留天数等参数。记录日志
Log 类快捷方法:Log::info('消息', ['上下文数据'])、Log::error('错误信息') 等。debug_backtrace() 记录调用栈。查看日志
runtime/log/ 目录下的日志文件(按天分割,默认格式为 YYYY-MM-DD.log)。php think log 查看所有日志,或结合 tail -f 实时监控。清理日志
runtime/log/ 下的日志文件。cron 定时任务,定期删除旧日志(如保留30天)。# 示例:每天凌晨清理30天前的日志
0 0 * * * find /path/to/project/runtime/log -name "*.log" -mtime +30 -exec rm -f {} \;
高级管理(可选)
think\log\driver\File 重写 format 方法。type => 'custom' 指定驱动类。runtime/log/ 目录权限为 755,避免敏感信息泄露。参考来源: