// Filename: internal/logging/logging.go package logging import ( "gemini-balancer/internal/config" "io" "os" "path/filepath" "github.com/sirupsen/logrus" ) 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'.") level = logrus.InfoLevel } logger.SetLevel(level) // 2. 设置日志格式 if cfg.Log.Format == "json" { logger.SetFormatter(&logrus.JSONFormatter{ TimestampFormat: "2006-01-02T15:04:05.000Z07:00", FieldMap: logrus.FieldMap{ logrus.FieldKeyTime: "timestamp", logrus.FieldKeyLevel: "level", logrus.FieldKeyMsg: "message", }, }) } else { logger.SetFormatter(&logrus.TextFormatter{ FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", }) } // 3. 设置日志输出 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.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.") 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 } // 同时输出到控制台和文件 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.") } else { // 仅输出到控制台 logger.SetOutput(os.Stdout) } logger.Info("Root logger initialized.") return logger }