refactor(logging): centralize log directory resolution logic

- Introduced `ResolveLogDirectory` function in `logging` package to standardize log directory determination across components.
- Replaced redundant logic in `server`, `global_logger`, and `handlers` with the new utility function.
This commit is contained in:
Luis Pater
2026-01-18 12:40:57 +08:00
parent 46433a25f8
commit 62e2b672d9
3 changed files with 22 additions and 21 deletions

View File

@@ -13,7 +13,7 @@ import (
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v6/internal/util" "github.com/router-for-me/CLIProxyAPI/v6/internal/logging"
) )
const ( const (
@@ -360,16 +360,7 @@ func (h *Handler) logDirectory() string {
if h.logDir != "" { if h.logDir != "" {
return h.logDir return h.logDir
} }
if base := util.WritablePath(); base != "" { return logging.ResolveLogDirectory(h.cfg)
return filepath.Join(base, "logs")
}
if h.configFilePath != "" {
dir := filepath.Dir(h.configFilePath)
if dir != "" && dir != "." {
return filepath.Join(dir, "logs")
}
}
return "logs"
} }
func (h *Handler) collectLogFiles(dir string) ([]string, error) { func (h *Handler) collectLogFiles(dir string) ([]string, error) {

View File

@@ -261,10 +261,7 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
if optionState.localPassword != "" { if optionState.localPassword != "" {
s.mgmt.SetLocalPassword(optionState.localPassword) s.mgmt.SetLocalPassword(optionState.localPassword)
} }
logDir := filepath.Join(s.currentPath, "logs") logDir := logging.ResolveLogDirectory(cfg)
if base := util.WritablePath(); base != "" {
logDir = filepath.Join(base, "logs")
}
s.mgmt.SetLogDirectory(logDir) s.mgmt.SetLogDirectory(logDir)
s.localPassword = optionState.localPassword s.localPassword = optionState.localPassword

View File

@@ -121,6 +121,24 @@ func isDirWritable(dir string) bool {
return true return true
} }
// ResolveLogDirectory determines the directory used for application logs.
func ResolveLogDirectory(cfg *config.Config) string {
logDir := "logs"
if base := util.WritablePath(); base != "" {
return filepath.Join(base, "logs")
}
if cfg == nil {
return logDir
}
if !isDirWritable(logDir) {
authDir := strings.TrimSpace(cfg.AuthDir)
if authDir != "" {
logDir = filepath.Join(authDir, "logs")
}
}
return logDir
}
// ConfigureLogOutput switches the global log destination between rotating files and stdout. // 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 // When logsMaxTotalSizeMB > 0, a background cleaner removes the oldest log files in the logs directory
// until the total size is within the limit. // until the total size is within the limit.
@@ -130,12 +148,7 @@ func ConfigureLogOutput(cfg *config.Config) error {
writerMu.Lock() writerMu.Lock()
defer writerMu.Unlock() defer writerMu.Unlock()
logDir := "logs" logDir := ResolveLogDirectory(cfg)
if base := util.WritablePath(); base != "" {
logDir = filepath.Join(base, "logs")
} else if !isDirWritable(logDir) {
logDir = filepath.Join(cfg.AuthDir, "logs")
}
protectedPath := "" protectedPath := ""
if cfg.LoggingToFile { if cfg.LoggingToFile {