diff --git a/docs/architecture/system-architecture.md b/docs/architecture/system-architecture.md index cc09df7..b6b8c26 100644 --- a/docs/architecture/system-architecture.md +++ b/docs/architecture/system-architecture.md @@ -317,8 +317,24 @@ func TraceMiddleware() gin.HandlerFunc { | 环境 | 最低级别 | 输出方式 | 格式 | |------|---------|---------|------| -| 开发 | DEBUG | 控制台(彩色) | 可读文本 | -| 生产 | INFO | 文件 + 控制台 | JSON 结构化 | +| 开发 | DEBUG | 控制台(彩色文本)+ 文件(JSON) | 控制台 text / 文件 JSON | +| 生产 | INFO | 控制台(JSON)+ 文件(JSON) | JSON 结构化 | + +**日志文件输出(基于 lumberjack 轮转):** + +| 文件 | 级别 | 用途 | +|------|------|------| +| `logs/app.log` | 全量(与配置级别一致) | 持久化存储、接入 ELK/Loki | +| `logs/error.log` | 仅 WARN + ERROR | 快速定位问题 | + +**日志文件轮转策略:** + +| 配置项 | 开发默认值 | 生产建议值 | 说明 | +|--------|-----------|-----------|------| +| `max_size` | 50 MB | 200 MB | 单文件最大大小,超过自动切割 | +| `max_backups` | 10 | 30 | 保留的旧日志文件数量 | +| `max_age` | 30 天 | 90 天 | 旧日志保留天数 | +| `compress` | false | true | 是否压缩归档文件 | ### 8.4 日志格式规范 diff --git a/docs/plans/2026-02-27-phase1-foundation-and-auth.md b/docs/plans/2026-02-27-phase1-foundation-and-auth.md index c0e1569..04f0714 100644 --- a/docs/plans/2026-02-27-phase1-foundation-and-auth.md +++ b/docs/plans/2026-02-27-phase1-foundation-and-auth.md @@ -181,9 +181,19 @@ type Config struct { } type LogConfig struct { - Level string `mapstructure:"level"` // debug/info/warn/error - Format string `mapstructure:"format"` // text(开发)/json(生产) - OutputPath string `mapstructure:"output_path"` // stdout 或文件路径 + Level string `mapstructure:"level"` // debug/info/warn/error + Format string `mapstructure:"format"` // text(开发)/json(生产) + OutputPath string `mapstructure:"output_path"` // stdout 或文件路径 + File LogFileConfig `mapstructure:"file"` // 日志文件轮转配置 +} + +type LogFileConfig struct { + Enable bool `mapstructure:"enable"` // 是否启用文件日志 + Dir string `mapstructure:"dir"` // 日志文件目录 + MaxSize int `mapstructure:"max_size"` // 单文件最大大小(MB) + MaxBackups int `mapstructure:"max_backups"` // 保留旧日志文件数量 + MaxAge int `mapstructure:"max_age"` // 旧日志保留天数 + Compress bool `mapstructure:"compress"` // 是否压缩归档文件 } ``` @@ -194,7 +204,11 @@ type LogConfig struct { - 支持从 context 中提取 trace_id 自动附加到每条日志 - 提供分级日志方法:`Debug(ctx, func, msg, fields...)` / `Info(...)` / `Warn(...)` / `Error(...)` - 敏感信息脱敏工具函数(邮箱、手机号、Token) -- 开发环境输出彩色可读文本,生产环境输出 JSON 结构化格式 +- 控制台输出(始终启用):开发环境彩色可读文本,生产环境 JSON 结构化 +- 文件输出(可配置启用):基于 lumberjack 自动轮转 + - `logs/app.log` — 全量日志(JSON 格式),便于接入 ELK/Loki + - `logs/error.log` — 仅 WARN 及以上级别,便于快速定位问题 + - 支持按文件大小切割、保留数量、过期天数、压缩归档 创建 `pkg/logs/trace.go`: - `GenerateTraceID()` — 生成唯一 trace ID(UUID v4 或雪花算法)