feat(log): B-03 日志轮转配置 - 添加 lumberjack 支持

- 添加 lumberjack.v2 依赖实现日志轮转
- 支持配置文件输出(stdout/stderr/file)
- 支持文件轮转(100MB/3备份/7天/压缩)
- 添加 Config 结构体灵活配置
- 添加完整测试用例

测试验证:
- TestInitWithConfig PASS
- TestInitWithConfigFileOutput PASS
- TestDefaultConfig PASS
- 全量日志测试通过
This commit is contained in:
phamnazage-jpg
2026-06-01 22:06:56 +08:00
parent 714c4acbe4
commit cf7dd35e1d
4 changed files with 120 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package log
import (
"log/slog"
"os"
"testing"
)
@@ -130,3 +131,59 @@ func TestRequestLogger(t *testing.T) {
t.Error("RequestLogger should not return nil")
}
}
func TestInitWithConfig(t *testing.T) {
// Test with stdout output
cfg := DefaultConfig()
cfg.Output = "stdout"
cfg.Level = "DEBUG"
InitWithConfig(cfg)
if logger == nil {
t.Error("logger should not be nil after InitWithConfig")
}
}
func TestInitWithConfigFileOutput(t *testing.T) {
// Test with file output (no rotation)
tmpFile := t.TempDir() + "/test.log"
cfg := DefaultConfig()
cfg.Output = tmpFile
cfg.Rotation = false
InitWithConfig(cfg)
Info("test message for file")
// Verify file was created
if _, err := os.Stat(tmpFile); os.IsNotExist(err) {
t.Errorf("log file %s should exist", tmpFile)
}
}
func TestDefaultConfig(t *testing.T) {
cfg := DefaultConfig()
if cfg.Level != "INFO" {
t.Errorf("default Level = %s, want INFO", cfg.Level)
}
if cfg.Output != "stdout" {
t.Errorf("default Output = %s, want stdout", cfg.Output)
}
if cfg.MaxSize != 100 {
t.Errorf("default MaxSize = %d, want 100", cfg.MaxSize)
}
if cfg.MaxBackups != 3 {
t.Errorf("default MaxBackups = %d, want 3", cfg.MaxBackups)
}
if cfg.MaxAge != 7 {
t.Errorf("default MaxAge = %d, want 7", cfg.MaxAge)
}
if !cfg.Compress {
t.Error("default Compress should be true")
}
}