This commit is contained in:
liangzai
2025-07-28 13:47:58 +08:00
parent d7df247e58
commit 90194bd803
11 changed files with 556 additions and 75 deletions

View File

@@ -83,12 +83,13 @@ type LoggerConfig struct {
Level string `mapstructure:"level"`
Format string `mapstructure:"format"`
Output string `mapstructure:"output"`
FilePath string `mapstructure:"file_path"`
MaxSize int `mapstructure:"max_size"`
MaxBackups int `mapstructure:"max_backups"`
MaxAge int `mapstructure:"max_age"`
Compress bool `mapstructure:"compress"`
UseColor bool `mapstructure:"use_color"`
LogDir string `mapstructure:"log_dir"` // 日志目录
MaxSize int `mapstructure:"max_size"` // 单个文件最大大小(MB)
MaxBackups int `mapstructure:"max_backups"` // 最大备份文件数
MaxAge int `mapstructure:"max_age"` // 最大保留天数
Compress bool `mapstructure:"compress"` // 是否压缩
UseColor bool `mapstructure:"use_color"` // 是否使用彩色输出
UseDaily bool `mapstructure:"use_daily"` // 是否按日分包
}
// JWTConfig JWT配置

View File

@@ -81,11 +81,12 @@ func NewContainer() *Container {
Level: cfg.Logger.Level,
Format: cfg.Logger.Format,
Output: cfg.Logger.Output,
FilePath: cfg.Logger.FilePath,
LogDir: cfg.Logger.LogDir,
MaxSize: cfg.Logger.MaxSize,
MaxBackups: cfg.Logger.MaxBackups,
MaxAge: cfg.Logger.MaxAge,
Compress: cfg.Logger.Compress,
UseDaily: cfg.Logger.UseDaily,
}
return logger.NewLogger(logCfg)
},

View File

@@ -4,9 +4,12 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gopkg.in/natefinch/lumberjack.v2"
)
// Logger 日志接口
@@ -33,11 +36,12 @@ type Config struct {
Level string
Format string
Output string
FilePath string
MaxSize int
MaxBackups int
MaxAge int
Compress bool
LogDir string // 日志目录
MaxSize int // 单个文件最大大小(MB)
MaxBackups int // 最大备份文件数
MaxAge int // 最大保留天数
Compress bool // 是否压缩
UseDaily bool // 是否按日分包
}
// NewLogger 创建新的日志实例
@@ -69,19 +73,10 @@ func NewLogger(config Config) (Logger, error) {
case "stderr":
writeSyncer = zapcore.AddSync(os.Stderr)
case "file":
if config.FilePath == "" {
config.FilePath = "logs/app.log"
}
// 确保目录存在
if err := os.MkdirAll("logs", 0755); err != nil {
return nil, fmt.Errorf("创建日志目录失败: %w", err)
}
file, err := os.OpenFile(config.FilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
writeSyncer, err = createFileWriteSyncer(config)
if err != nil {
return nil, fmt.Errorf("打开日志文件失败: %w", err)
return nil, fmt.Errorf("创建文件输出失败: %w", err)
}
writeSyncer = zapcore.AddSync(file)
default:
writeSyncer = zapcore.AddSync(os.Stdout)
}
@@ -97,6 +92,56 @@ func NewLogger(config Config) (Logger, error) {
}, nil
}
// createFileWriteSyncer 创建文件输出同步器
func createFileWriteSyncer(config Config) (zapcore.WriteSyncer, error) {
// 设置默认日志目录
if config.LogDir == "" {
config.LogDir = "logs"
}
// 确保日志目录存在
if err := os.MkdirAll(config.LogDir, 0755); err != nil {
return nil, fmt.Errorf("创建日志目录失败: %w", err)
}
// 设置默认值
if config.MaxSize == 0 {
config.MaxSize = 100 // 默认100MB
}
if config.MaxBackups == 0 {
config.MaxBackups = 3 // 默认3个备份
}
if config.MaxAge == 0 {
config.MaxAge = 7 // 默认7天
}
// 构建日志文件路径
var logFilePath string
if config.UseDaily {
// 按日分包logs/2024-01-01/app.log
today := time.Now().Format("2006-01-02")
dailyDir := filepath.Join(config.LogDir, today)
if err := os.MkdirAll(dailyDir, 0755); err != nil {
return nil, fmt.Errorf("创建日期目录失败: %w", err)
}
logFilePath = filepath.Join(dailyDir, "app.log")
} else {
// 传统方式logs/app.log
logFilePath = filepath.Join(config.LogDir, "app.log")
}
// 创建lumberjack日志轮转器
lumberJackLogger := &lumberjack.Logger{
Filename: logFilePath,
MaxSize: config.MaxSize, // 单个文件最大大小(MB)
MaxBackups: config.MaxBackups, // 最大备份文件数
MaxAge: config.MaxAge, // 最大保留天数
Compress: config.Compress, // 是否压缩
}
return zapcore.AddSync(lumberJackLogger), nil
}
// getEncoderConfig 获取编码器配置
func getEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{