用户日志表,用于给用户看的日志表,记录用户相关的信息
用户日志系统是一个专门为用户提供操作记录查看的功能模块,主要用于记录和展示用户在游戏中的各种操作和变更信息。
CREATE TABLE `kku_user_logs` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`user_id` int NOT NULL COMMENT '用户ID',
`message` text NOT NULL COMMENT '日志消息内容',
`source_type` varchar(50) DEFAULT NULL COMMENT '来源类型(fund, item, farm等)',
`source_id` int DEFAULT NULL COMMENT '来源记录ID',
`source_table` varchar(100) DEFAULT NULL COMMENT '来源表名',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (`id`),
KEY `idx_user_id` (`user_id`),
KEY `idx_created_at` (`created_at`),
KEY `idx_source` (`source_type`, `source_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户日志表';
| 字段名 | 类型 | 说明 |
|---|---|---|
| id | bigint unsigned | 主键ID,自增 |
| user_id | int | 用户ID,关联用户表 |
| message | text | 日志消息内容,用户友好的文字描述 |
| source_type | varchar(50) | 来源类型,如fund、item、farm等 |
| source_id | int | 来源记录ID,关联具体的业务记录 |
| source_table | varchar(100) | 来源表名,便于追溯原始数据 |
| created_at | timestamp | 创建时间 |
请求消息:RequestUserLogData
page:分页信息响应消息:ResponseUserLogdata
page:分页信息logs:日志列表,每个日志包含:
msg:日志消息time:时间请求消息:RequestUserClearLog
响应消息:ResponseUserClearLog
app/Module/Game/
├── Models/
│ └── UserLog.php # 用户日志模型
├── Services/
│ ├── UserLogService.php # 用户日志服务
│ └── UserLogScheduleService.php # 日志调度服务
├── Logics/
│ ├── UserLogLogic.php # 用户日志逻辑
│ ├── UserLogCollectorManager.php # 收集器管理器
│ └── UserLogCollectors/ # 日志收集器目录
│ ├── BaseLogCollector.php # 收集器基类
│ ├── FundLogCollector.php # 资金日志收集器
│ ├── ItemLogCollector.php # 物品日志收集器
│ └── FarmLogCollector.php # 农场日志收集器
├── Jobs/
│ └── CollectUserLogJob.php # 日志收集任务
├── Commands/
│ ├── CollectUserLogsCommand.php # 日志收集命令
│ └── CleanExpiredUserLogsCommand.php # 清理过期日志命令
└── AdminControllers/
└── UserLogController.php # 后台管理控制器
app/Module/AppGame/Handler/User/
├── LogDataHandler.php # 用户日志数据Handler
└── ClearLogHandler.php # 清空日志Handler
系统采用计划任务架构,通过定时扫描各模块的日志表来收集用户日志:
获得{资金名称} {数量}消耗{资金名称} {数量}获得金币 1000、消耗钻石 50获得{物品名称} {数量}消耗{物品名称} {数量}获得普通种子 100、消耗除草剂 1在{土地编号}种植{作物名称}收获{土地编号}的{作物名称},获得{产出}房屋升级到{等级}级获得宠物{宠物名称}宠物{宠物名称}升级到{等级}级宠物{宠物名称}使用技能{技能名称}// 命令行方式
php artisan game:collect-user-logs
// 指定收集器
php artisan game:collect-user-logs --collector=fund
// 重置进度
php artisan game:collect-user-logs --reset
// 程序调用
UserLogScheduleService::collectNow(); // 同步执行
UserLogScheduleService::scheduleCollection(); // 异步执行
// 获取用户日志列表
$logs = UserLogService::getUserLogs($userId, $page, $pageSize);
// 清空用户日志
UserLogService::clearUserLogs($userId);
// 获取收集器状态
$status = UserLogScheduleService::checkCollectorsStatus();
支持配置化的消息模板,便于国际化和个性化:
$templates = [
'fund.gain' => '获得{fund_name} {amount}',
'fund.cost' => '消耗{fund_name} {amount}',
'item.gain' => '获得{item_name} {quantity}',
// ...
];
支持配置哪些事件需要记录日志,避免记录过多无用信息:
$logRules = [
'fund' => ['min_amount' => 100], // 只记录100以上的资金变更
'item' => ['exclude_types' => ['temp']], // 排除临时物品
// ...
];
使用计划任务定时扫描各模块的原始日志表:
// 每2秒执行一次日志收集
* * * * * php artisan game:collect-user-logs
// 或者通过队列异步执行
CollectUserLogJob::dispatchCollection();
每个模块都有对应的日志收集器,继承自基类:
class FundLogCollector extends BaseLogCollector
{
protected string $sourceTable = 'fund_logs';
protected string $sourceType = 'fund';
protected function getNewRecords(int $lastProcessedId)
{
return FundLogModel::where('id', '>', $lastProcessedId)
->orderBy('id')
->limit($this->maxRecords)
->get();
}
protected function convertToUserLog($record): ?array
{
// 转换逻辑
}
}
使用缓存记录每个收集器的处理进度:
// 获取上次处理的最大ID
$lastProcessedId = Cache::get("user_log_collector:last_processed_id:{$sourceTable}", 0);
// 更新处理进度
Cache::put("user_log_collector:last_processed_id:{$sourceTable}", $newId, 86400);
用户日志系统是游戏中重要的功能模块,通过合理的设计和实现,可以为用户提供良好的操作记录查看体验,同时为系统运维提供有价值的数据支持。