From bc866bac4962eb92d7e702f3c2d3648c9f1baae9 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Tue, 30 Dec 2025 10:28:25 +0800 Subject: [PATCH] fix(logging): refactor `ConfigureLogOutput` to accept config object and adjust log directory handling --- cmd/server/main.go | 2 +- internal/api/server.go | 2 +- internal/logging/global_logger.go | 18 ++++++------------ 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/cmd/server/main.go b/cmd/server/main.go index 2b20bcb5..f9bb2080 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -405,7 +405,7 @@ func main() { usage.SetStatisticsEnabled(cfg.UsageStatisticsEnabled) coreauth.SetQuotaCooldownDisabled(cfg.DisableCooling) - if err = logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB); err != nil { + if err = logging.ConfigureLogOutput(cfg); err != nil { log.Errorf("failed to configure log output: %v", err) return } diff --git a/internal/api/server.go b/internal/api/server.go index d5139f54..bac13759 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -856,7 +856,7 @@ func (s *Server) UpdateClients(cfg *config.Config) { } if oldCfg == nil || oldCfg.LoggingToFile != cfg.LoggingToFile || oldCfg.LogsMaxTotalSizeMB != cfg.LogsMaxTotalSizeMB { - if err := logging.ConfigureLogOutput(cfg.LoggingToFile, cfg.LogsMaxTotalSizeMB); err != nil { + if err := logging.ConfigureLogOutput(cfg); err != nil { log.Errorf("failed to reconfigure log output: %v", err) } else { if oldCfg == nil { diff --git a/internal/logging/global_logger.go b/internal/logging/global_logger.go index 290849e0..d2fbd425 100644 --- a/internal/logging/global_logger.go +++ b/internal/logging/global_logger.go @@ -10,6 +10,7 @@ import ( "sync" "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" "github.com/router-for-me/CLIProxyAPI/v6/internal/util" log "github.com/sirupsen/logrus" "gopkg.in/natefinch/lumberjack.v2" @@ -86,7 +87,7 @@ func SetupBaseLogger() { // ConfigureLogOutput switches the global log destination between rotating files and stdout. // When logsMaxTotalSizeMB > 0, a background cleaner removes the oldest log files in the logs directory // until the total size is within the limit. -func ConfigureLogOutput(loggingToFile bool, logsMaxTotalSizeMB int) error { +func ConfigureLogOutput(cfg *config.Config) error { SetupBaseLogger() writerMu.Lock() @@ -95,19 +96,12 @@ func ConfigureLogOutput(loggingToFile bool, logsMaxTotalSizeMB int) error { logDir := "logs" if base := util.WritablePath(); base != "" { logDir = filepath.Join(base, "logs") - } else if loggingToFile { - // When logging to file is enabled but WRITABLE_PATH is not set, - // use a default writable location to avoid errors on read-only filesystems - // (e.g., Homebrew installations on macOS). - home, err := os.UserHomeDir() - if err != nil { - return fmt.Errorf("logging: failed to determine user home directory for fallback log path: %w", err) - } - logDir = filepath.Join(home, ".cliproxyapi", "logs") + } else { + logDir = filepath.Join(cfg.AuthDir, "logs") } protectedPath := "" - if loggingToFile { + if cfg.LoggingToFile { if err := os.MkdirAll(logDir, 0o755); err != nil { return fmt.Errorf("logging: failed to create log directory: %w", err) } @@ -131,7 +125,7 @@ func ConfigureLogOutput(loggingToFile bool, logsMaxTotalSizeMB int) error { log.SetOutput(os.Stdout) } - configureLogDirCleanerLocked(logDir, logsMaxTotalSizeMB, protectedPath) + configureLogDirCleanerLocked(logDir, cfg.LogsMaxTotalSizeMB, protectedPath) return nil }