ThinkPHP中的日志通道配置

在 ThinkPHP 中配置日志通道主要通过 config/log.php 文件实现。以下是详细配置说明和示例:


1. 基础配置结构

// config/log.php
return [
    'default' => 'file',  // 默认使用的通道
    'channels' => [
        // 通道1:文件日志(默认)
        'file' => [
            'type' => 'file',
            'path' => runtime_path('log'),
            'level' => ['info', 'error', 'critical'],
            'max_files' => 30,
            'json' => false,
            'apart_level' => [], // 单独记录的日志级别
        ],
        
        // 通道2:数据库日志
        'db' => [
            'type' => 'database',
            'table' => 'sys_logs',   // 数据库表名
            'connection' => 'mysql', // 数据库连接配置
        ],
        
        // 更多通道...
    ]
];

2. 核心配置参数说明

通用参数
参数名 说明 示例值
type 驱动类型 file/database/test/自定义类
level 记录级别 ['error', 'warning']
文件日志专有参数
参数名 说明 示例值
path 日志目录 runtime_path('log')
max_files 最大文件数(0=不限) 30
json JSON格式记录 true/false
apart_level 单独文件记录级别 ['error', 'sql']
single 单一文件模式 true(固定文件名)
file_size 文件大小限制(MB) 10(按大小分割)
数据库日志专有参数
参数名 说明 示例值
table 日志表名 'system_logs'
connection 数据库连接 null(默认连接)

3. 多通道配置示例

(1) 开发环境配置(详细日志)
'channels' => [
    'dev_debug' => [
        'type' => 'file',
        'path' => runtime_path('log/dev'),
        'level' => ['debug', 'info', 'error'],
        'apart_level' => ['sql'],  // SQL日志单独文件
        'max_files' => 7,          // 保留7天日志
    ]
]
(2) 生产环境配置(分离错误日志)
'prod' => [
    'type' => 'file',
    'path' => runtime_path('log'),
    'level' => ['error', 'critical', 'emergency'],
    'apart_level' => ['error'],  // error级别单独记录
    'max_files' => 90,           // 保留90天
    'file_size' => 20,           // 每个文件最大20MB
]
(3) 自定义日志驱动
'email_alert' => [
    'type' => \app\log\driver\EmailLogger::class, // 自定义类
    'receivers' => ['[email protected]'],
    'level' => ['emergency', 'critical']
]
(4) JSON格式日志(ELK集成)
'json_log' => [
    'type' => 'file',
    'path' => runtime_path('log/json'),
    'json' => true,  // 开启JSON格式
    'level' => ['info', 'error']
]

4. 动态切换通道

(1) 代码中临时切换
// 使用指定通道记录
Log::channel('email_alert')->emergency('数据库崩溃!');

// 同时写入多个通道
Log::channel(['file', 'db'])->info('订单创建', $orderData);
(2) 配置环境变量切换

.env 文件中设置:

APP_LOG_CHANNEL = prod

在配置中读取:

'default' => env('APP_LOG_CHANNEL', 'file'),

5. 高级配置:日志通道继承

'channels' => [
    // 基础配置
    'base' => [
        'type' => 'file',
        'path' => runtime_path('log'),
        'max_files' => 30
    ],
    
    // 业务日志(继承基础配置)
    'order' => [
        'driver' => 'base',  // 继承配置
        'path' => runtime_path('log/order'), // 覆盖路径
        'level' => ['info', 'error']
    ],
    
    // 支付日志
    'payment' => [
        'driver' => 'base',
        'path' => runtime_path('log/payment'),
        'apart_level' => ['error']
    ]
]

6. 验证配置

创建测试路由检查配置:

// 测试路由
Route::get('log-test', function(){
    Log::channel('email_alert')->debug('测试邮件通道');
    Log::channel('json_log')->info('JSON格式测试', ['user' => 'admin']);
    return '日志测试完成';
});

检查对应日志文件或数据库确认写入结果。


典型场景配置方案

场景 推荐配置
开发调试 多级别日志 + SQL分离
生产环境 错误日志分离 + 长期保留
微服务架构 JSON格式 + 日志收集系统
安全审计 数据库存储 + 独立通道
实时监控 自定义驱动(邮件/钉钉)
高并发场景 文件日志 + Swoole异步写入

通过灵活组合这些配置选项,可以构建出适合各种业务场景的日志解决方案。

你可能感兴趣的:(PHP,经验,ThinkPHP,php,后端,日志)