CentOS 服务器上 ThinkPHP 日志管理
一 基础配置与存放路径
return [
'default' => 'file',
'channels' => [
'file' => [
'type' => 'file',
'path' => '', // 默认 runtime/log
'level' => ['error','info'], // 只记录 error、info
'max_files' => 30, // 最多保留 30 个文件
'json' => false,
],
],
];
use think\facade\Log;
Log::info('用户登录', ['user_id' => 1]);
Log::error('支付失败', ['order_id' => 1001, 'reason' => '余额不足']);
tail -f runtime/log/2025-07-07.log。高频日志可开启批量写入:Log::batch(true)。二 写入策略与保留策略
# 每天 01:00 归档
0 1 * * * cd /path/to/project && php think log:archive
'type' => 'test'。三 服务器侧日志轮转与清理
/path/to/project/runtime/log/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0755 www-data www-data
copytruncate
}
说明:
logrotate -vf /etc/logrotate.dphp。journalctl --vacuum-time=7d 或 journalctl --vacuum-size=100M。四 常见问题与排查