installerbuilder/tasks/task02-日志系统实现.md

99 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 任务02日志系统实现
## 任务描述
实现Installer Builder项目的日志系统包括Logger接口及其默认实现。日志系统将支持不同的日志级别、格式化输出和上下文信息为整个项目提供统一的日志记录机制。
## 实现步骤
1. 定义Logger接口
- 在`internal/common/log`包中创建`logger.go`文件
- 定义Logger接口包含以下方法
```go
type Logger interface {
Debug(msg string, args ...interface{})
Info(msg string, args ...interface{})
Warn(msg string, args ...interface{})
Error(msg string, args ...interface{})
Fatal(msg string, args ...interface{})
WithField(key string, value interface{}) Logger
WithFields(fields map[string]interface{}) Logger
}
```
2. 定义日志级别:
- 创建LogLevel类型和常量
```go
type LogLevel int
const (
DebugLevel LogLevel = iota
InfoLevel
WarnLevel
ErrorLevel
FatalLevel
)
```
- 实现LogLevel的String()方法,用于字符串表示
3. 实现DefaultLogger
- 创建DefaultLogger结构体实现Logger接口
- 支持配置日志级别、输出目标和格式
- 实现字段上下文支持WithField和WithFields方法
- 支持彩色输出(可选,根据终端类型)
4. 实现日志格式化:
- 支持不同的日志格式文本、JSON
- 包含时间戳、日志级别、消息和字段信息
- 实现格式化器接口,允许自定义格式
5. 实现日志输出:
- 支持多种输出目标(控制台、文件、网络)
- 实现输出接口,允许自定义输出目标
- 支持同时输出到多个目标
6. 创建日志工厂:
- 实现NewLogger函数用于创建Logger实例
- 支持从配置创建Logger
- 提供默认的全局Logger实例
7. 实现日志上下文传递:
- 支持从父Logger创建子Logger继承上下文
- 实现上下文合并功能
## 单元测试要求
1. 测试Logger接口实现
- 验证所有方法按预期工作
- 测试不同日志级别的过滤
2. 测试日志格式化:
- 验证不同格式的输出正确性
- 测试特殊字符和多行消息的处理
3. 测试日志输出:
- 验证不同输出目标的正确性
- 测试并发写入的安全性
4. 测试上下文功能:
- 验证WithField和WithFields方法
- 测试上下文继承和合并
5. 测试性能:
- 基准测试不同日志级别和格式的性能
- 测试高并发场景下的性能
## 依赖关系
- 依赖任务01项目初始化
- 被任务03错误处理框架依赖
## 完成标准
1. Logger接口已定义并文档完善
2. DefaultLogger实现了所有接口方法
3. 支持所有指定的日志级别
4. 支持字段上下文和格式化
5. 支持多种输出目标
6. 所有单元测试通过
7. 性能满足项目需求
8. 代码符合项目的Go语言开发规范