diff --git a/backend/go-service/cmd/server/main.go b/backend/go-service/cmd/server/main.go index 4d938ba..ec990ed 100644 --- a/backend/go-service/cmd/server/main.go +++ b/backend/go-service/cmd/server/main.go @@ -28,7 +28,7 @@ func main() { } // 2. 初始化日志系统 - if err := logs.Init(cfg.Log.Level, cfg.Log.Format, cfg.Log.OutputPath); err != nil { + if err := logs.Init(&cfg.Log); err != nil { fmt.Printf("初始化日志失败: %v\n", err) os.Exit(1) } diff --git a/backend/go-service/config/config.dev.yaml b/backend/go-service/config/config.dev.yaml index ff9db77..4b71be0 100644 --- a/backend/go-service/config/config.dev.yaml +++ b/backend/go-service/config/config.dev.yaml @@ -1,5 +1,5 @@ server: - port: 8080 + port: 8085 mode: debug database: @@ -28,3 +28,10 @@ log: level: debug format: text output_path: stdout + file: + enable: true + dir: logs + max_size: 50 + max_backups: 10 + max_age: 30 + compress: false diff --git a/backend/go-service/config/config.go b/backend/go-service/config/config.go index e6aaeb4..4a25ae3 100644 --- a/backend/go-service/config/config.go +++ b/backend/go-service/config/config.go @@ -59,17 +59,28 @@ func (r *RedisConfig) Addr() string { // JWTConfig JWT 认证配置 type JWTConfig struct { - Secret string `mapstructure:"secret"` // 签名密钥 - AccessExpireMin int `mapstructure:"access_expire_min"` // Access Token 有效期(分钟) + Secret string `mapstructure:"secret"` // 签名密钥 + AccessExpireMin int `mapstructure:"access_expire_min"` // Access Token 有效期(分钟) RefreshExpireDay int `mapstructure:"refresh_expire_day"` // Refresh Token 有效期(天) Issuer string `mapstructure:"issuer"` // 签发者 } // LogConfig 日志配置 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"` // 日志文件轮转配置 +} + +// LogFileConfig 日志文件轮转配置(基于 lumberjack) +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"` // 是否压缩归档的旧日志文件 } // Load 加载配置文件并返回 Config 实例 diff --git a/backend/go-service/go.mod b/backend/go-service/go.mod index 703117f..848a855 100644 --- a/backend/go-service/go.mod +++ b/backend/go-service/go.mod @@ -9,6 +9,7 @@ require ( github.com/redis/go-redis/v9 v9.18.0 github.com/spf13/viper v1.21.0 go.uber.org/zap v1.27.1 + gopkg.in/natefinch/lumberjack.v2 v2.2.1 gorm.io/driver/postgres v1.6.0 gorm.io/gorm v1.31.1 ) diff --git a/backend/go-service/go.sum b/backend/go-service/go.sum index ba1270a..c3c0f66 100644 --- a/backend/go-service/go.sum +++ b/backend/go-service/go.sum @@ -150,6 +150,8 @@ google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXn gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=