PHP项目ThinkPHP日志通道配置

wen PHP项目 3

本文目录导读:

PHP项目ThinkPHP日志通道配置

  1. 基础配置
  2. 多环境配置
  3. 日志通道类型
  4. 日志记录器配置
  5. 使用示例
  6. 高级配置示例
  7. 最佳实践建议
  8. 注意事项

在ThinkPHP框架中,日志通道配置是非常重要的一部分,我来详细介绍一下如何配置和使用。

基础配置

配置文件位置

config/log.php 是日志的主配置文件,通常包含以下内容:

<?php
return [
    // 默认日志记录通道
    'default'      => env('log.channel', 'file'),
    // 日志记录级别
    'level'        => ['error', 'warning', 'notice', 'info', 'debug', 'sql'],
    // 日志类型分类
    'type_channel' => [
        // 可以将不同的日志类型分配到不同的通道
        // 'info' => 'email',
        // 'error' => 'email',
    ],
    // 关闭全局日志写入
    'close'        => false,
    // 日志通道列表
    'channels'     => [
        'file' => [
            // 日志记录方式
            'type'           => 'File',
            // 日志保存目录
            'path'           => '',
            // 单文件日志写入
            'single'         => false,
            // 独立日志级别
            'apart_level'    => [],
            // 最大日志文件数量
            'max_files'      => 0,
            // 使用JSON格式记录
            'json'           => false,
            // 日志处理
            'processor'      => null,
            // 关闭通道日志写入
            'close'          => false,
            // 日志输出格式化
            'format'         => '[%s][%s] %s',
            // 是否实时写入
            'realtime_write' => false,
        ],
        'error' => [
            'type'        => 'File',
            'path'        => './logs/error/',
            'apart_level' => ['error'],
        ],
        'sql' => [
            'type'        => 'File',
            'path'        => './logs/sql/',
            'apart_level' => ['sql'],
        ],
        'daily' => [
            'type'        => 'File',
            'path'        => './logs/daily/',
            'single'      => false,
            'max_files'   => 30, // 保留30天的日志
        ],
        // 邮件日志通道
        'email' => [
            'type'  => 'email',
            'host'  => 'smtp.example.com',
            'port'  => 465,
            'user'  => 'your-email@example.com',
            'pass'  => 'your-password',
            'to'    => ['admin@example.com'],
            'level' => ['error', 'critical'],
        ],
    ],
];

多环境配置

开发环境配置

.env 文件中:

# 开发环境使用文件日志
log.channel=file
log.level=debug
log.path=./runtime/logs/dev/

生产环境配置

# 生产环境使用每日日志
log.channel=daily
log.level=error
log.path=./runtime/logs/prod/

日志通道类型

文件日志

'file' => [
    'type'           => 'File',
    'path'           => './runtime/logs/',
    'single'         => false,        // 是否单文件
    'apart_level'    => ['error', 'sql'], // 独立级别的日志
    'max_files'      => 30,           // 最多保留文件数
    'json'           => false,        // JSON格式
    'format'         => '[%s][%s] %s', // 格式化
    'realtime_write' => false,        // 实时写入
],

Socket日志(远程日志)

'socket' => [
    'type' => 'Socket',
    'host' => '192.168.1.100',
    'port' => 9501,
    'debug' => false,
],

自定义通道

'custom' => [
    'type'  => 'Custom',
    'class' => \app\common\log\MyLogger::class,
    'config' => [
        // 自定义配置参数
        'api_url' => 'http://your-log-server/api',
        'api_key' => 'your-api-key',
    ],
],

日志记录器配置

自定义日志处理器

// config/log.php
'channels' => [
    'file' => [
        'type'      => 'File',
        'processor' => function($log) {
            // 对日志进行额外处理
            $log['extra'] = [
                'ip' => request()->ip(),
                'user_id' => session('user_id'),
            ];
            return $log;
        },
    ],
],

使用示例

基础日志记录

use think\facade\Log;
// 记录不同级别的日志
Log::info('这是一条信息');
Log::error('这是一条错误');
Log::warning('这是一条警告');
Log::debug('调试信息', ['param' => 'value']);
// 使用数组记录
Log::write([
    'module' => 'user',
    'action' => 'login',
    'message' => '用户登录成功',
]);
// 指定级别写入
Log::record('这是一条SQL日志', 'sql');

动态切换通道

use think\facade\Log;
// 仅为当前请求切换日志通道
Log::channel('daily')->info('今日日志');
// 写入特定通道
Log::channel('error')->error('错误日志');
Log::channel('sql')->info('SQL日志', ['sql' => $sql]);

高级配置示例

完整的日志配置示例

<?php
return [
    'default'      => env('log.channel', 'file'),
    'level'        => ['error', 'warning', 'notice', 'info'],
    'channels'     => [
        // 默认文件日志
        'file' => [
            'type'           => 'File',
            'path'           => app()->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'logs',
            'single'         => false,
            'apart_level'    => ['error'],
            'max_files'      => 0,
            'json'           => false,
            'format'         => '[%s][%s] %s',
            'realtime_write' => false,
        ],
        // 每日日志(按天存储)
        'daily' => [
            'type'      => 'File',
            'path'      => './runtime/logs/daily/',
            'single'    => false,
            'max_files' => 90, // 保留90天
            'format'    => '[%s][%s] %s',
        ],
        // 错误日志(专门记录错误)
        'error' => [
            'type'        => 'File',
            'path'        => './runtime/logs/error/',
            'apart_level' => ['error', 'critical', 'alert', 'emergency'],
        ],
        // SQL日志
        'sql' => [
            'type'        => 'File',
            'path'        => './runtime/logs/sql/',
            'apart_level' => ['sql'],
        ],
        // 邮件日志
        'email' => [
            'type'        => 'email',
            'host'        => env('MAIL_HOST', 'smtp.example.com'),
            'port'        => env('MAIL_PORT', 465),
            'user'        => env('MAIL_USER'),
            'pass'        => env('MAIL_PASSWORD'),
            'to'          => [env('ADMIN_EMAIL')],
            'level'       => ['error', 'critical'],
        ],
    ],
    // 根据错误级别切换通道
    'type_channel' => [
        'error' => 'email',
        'critical' => 'email',
        'sql' => 'sql',
    ],
];

最佳实践建议

  1. 开发环境:使用 debug 级别,记录所有日志
  2. 测试环境:使用 info 级别,记录关键操作
  3. 生产环境:只记录 errorwarning,避免过多IO操作
  4. 日志轮转:设置合理的 max_files 或使用 daily 通道
  5. 敏感信息过滤:在生产环境不要记录敏感数据(如密码、Token等)

注意事项

  • 日志目录需要有写入权限
  • 生产环境合理控制日志级别
  • 定期清理过期日志
  • 对于高并发场景,考虑使用异步日志或外部日志服务

通过合理配置日志通道,可以更好的监控和管理你的ThinkPHP应用。

抱歉,评论功能暂时关闭!