Fix Services & Update the middleware && others

This commit is contained in:
XOF
2025-11-24 04:48:07 +08:00
parent 3a95a07e8a
commit f2706d6fc8
37 changed files with 4458 additions and 1166 deletions

View File

@@ -9,20 +9,25 @@ import (
"path/filepath"
"github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
)
// 包级变量,用于存储日志轮转器
var logRotator *lumberjack.Logger
// NewLogger 返回标准的 *logrus.Logger兼容 Fx 依赖注入)
func NewLogger(cfg *config.Config) *logrus.Logger {
logger := logrus.New()
// 1. 设置日志级别
// 设置日志级别
level, err := logrus.ParseLevel(cfg.Log.Level)
if err != nil {
logger.WithField("configured_level", cfg.Log.Level).Warn("Invalid log level specified, defaulting to 'info'.")
logger.WithField("configured_level", cfg.Log.Level).Warn("Invalid log level, defaulting to 'info'")
level = logrus.InfoLevel
}
logger.SetLevel(level)
// 2. 设置日志格式
// 设置日志格式
if cfg.Log.Format == "json" {
logger.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: "2006-01-02T15:04:05.000Z07:00",
@@ -39,36 +44,57 @@ func NewLogger(cfg *config.Config) *logrus.Logger {
})
}
// 3. 设置日志输出
// 添加全局字段
hostname, _ := os.Hostname()
logger = logger.WithFields(logrus.Fields{
"service": "gemini-balancer",
"hostname": hostname,
}).Logger
// 设置日志输出
if cfg.Log.EnableFile {
if cfg.Log.FilePath == "" {
logger.Warn("Log file is enabled but no file path is specified. Logging to console only.")
logger.Warn("Log file enabled but no path specified. Logging to console only")
logger.SetOutput(os.Stdout)
return logger
}
logDir := filepath.Dir(cfg.Log.FilePath)
if err := os.MkdirAll(logDir, 0755); err != nil {
logger.WithError(err).Warn("Failed to create log directory. Logging to console only.")
if err := os.MkdirAll(logDir, 0750); err != nil {
logger.WithError(err).Warn("Failed to create log directory. Logging to console only")
logger.SetOutput(os.Stdout)
return logger
}
logFile, err := os.OpenFile(cfg.Log.FilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
logger.WithError(err).Warn("Failed to open log file. Logging to console only.")
logger.SetOutput(os.Stdout)
return logger
// 配置日志轮转(保存到包级变量)
logRotator = &lumberjack.Logger{
Filename: cfg.Log.FilePath,
MaxSize: getOrDefault(cfg.Log.MaxSize, 100),
MaxBackups: getOrDefault(cfg.Log.MaxBackups, 7),
MaxAge: getOrDefault(cfg.Log.MaxAge, 30),
Compress: cfg.Log.Compress,
}
// 同时输出到控制台和文件
logger.SetOutput(io.MultiWriter(os.Stdout, logFile))
logger.WithField("log_file_path", cfg.Log.FilePath).Info("Logging is now configured to output to both console and file.")
logger.SetOutput(io.MultiWriter(os.Stdout, logRotator))
logger.WithField("log_file", cfg.Log.FilePath).Info("Logging to both console and file")
} else {
// 仅输出到控制台
logger.SetOutput(os.Stdout)
}
logger.Info("Root logger initialized.")
logger.Info("Logger initialized successfully")
return logger
}
// Close 关闭日志轮转器(在 main.go 中调用)
func Close() {
if logRotator != nil {
logRotator.Close()
}
}
func getOrDefault(value, defaultValue int) int {
if value <= 0 {
return defaultValue
}
return value
}