From c875088be2b938757d9236624bb55a29313ddd57 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Mon, 8 Sep 2025 01:09:39 +0800 Subject: [PATCH] Add dynamic log level adjustment and "type" field to auth files response - Introduced `SetLogLevel` utility function for unified log level management. - Updated dynamic log level handling across server and watcher components. - Extended auth files response by extracting and including the `type` field from file content. - Updated management API documentation with the new `type` field in auth files response. --- MANAGEMENT_API.md | 2 +- MANAGEMENT_API_CN.md | 2 +- cmd/server/main.go | 7 ++---- .../api/handlers/management/auth_files.go | 12 +++++++++- internal/api/server.go | 7 ++---- internal/util/proxy.go | 2 +- internal/util/util.go | 23 +++++++++++++++++++ internal/watcher/watcher.go | 8 +++++++ 8 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 internal/util/util.go diff --git a/MANAGEMENT_API.md b/MANAGEMENT_API.md index aaba0902..17f6873e 100644 --- a/MANAGEMENT_API.md +++ b/MANAGEMENT_API.md @@ -466,7 +466,7 @@ Manage JSON token files under `auth-dir`: list, download, upload, delete. ``` - Response: ```json - { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z" } ] } + { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z", "type": "google" } ] } ``` - GET `/auth-files/download?name=` — Download a single file diff --git a/MANAGEMENT_API_CN.md b/MANAGEMENT_API_CN.md index 09ea0594..b4a661fa 100644 --- a/MANAGEMENT_API_CN.md +++ b/MANAGEMENT_API_CN.md @@ -466,7 +466,7 @@ ``` - 响应: ```json - { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z" } ] } + { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z", "type": "google" } ] } ``` - GET `/auth-files/download?name=` — 下载单个文件 diff --git a/cmd/server/main.go b/cmd/server/main.go index c5341cbf..9c8c2809 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -14,6 +14,7 @@ import ( "github.com/luispater/CLIProxyAPI/internal/cmd" "github.com/luispater/CLIProxyAPI/internal/config" _ "github.com/luispater/CLIProxyAPI/internal/translator" + "github.com/luispater/CLIProxyAPI/internal/util" log "github.com/sirupsen/logrus" ) @@ -112,11 +113,7 @@ func main() { } // Set the log level based on the configuration. - if cfg.Debug { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) - } + util.SetLogLevel(cfg) // Expand the tilde (~) in the auth directory path to the user's home directory. if strings.HasPrefix(cfg.AuthDir, "~") { diff --git a/internal/api/handlers/management/auth_files.go b/internal/api/handlers/management/auth_files.go index 6095389e..6337b3da 100644 --- a/internal/api/handlers/management/auth_files.go +++ b/internal/api/handlers/management/auth_files.go @@ -8,6 +8,7 @@ import ( "strings" "github.com/gin-gonic/gin" + "github.com/tidwall/gjson" ) // List auth files @@ -27,7 +28,16 @@ func (h *Handler) ListAuthFiles(c *gin.Context) { continue } if info, errInfo := e.Info(); errInfo == nil { - files = append(files, gin.H{"name": name, "size": info.Size(), "modtime": info.ModTime()}) + fileData := gin.H{"name": name, "size": info.Size(), "modtime": info.ModTime()} + + // Read file to get type field + full := filepath.Join(h.cfg.AuthDir, name) + if data, errRead := os.ReadFile(full); errRead == nil { + typeValue := gjson.GetBytes(data, "type").String() + fileData["type"] = typeValue + } + + files = append(files, fileData) } } c.JSON(200, gin.H{"files": files}) diff --git a/internal/api/server.go b/internal/api/server.go index 9828189f..3af272b3 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -22,6 +22,7 @@ import ( "github.com/luispater/CLIProxyAPI/internal/config" "github.com/luispater/CLIProxyAPI/internal/interfaces" "github.com/luispater/CLIProxyAPI/internal/logging" + "github.com/luispater/CLIProxyAPI/internal/util" log "github.com/sirupsen/logrus" ) @@ -305,11 +306,7 @@ func (s *Server) UpdateClients(clients map[string]interfaces.Client, cfg *config // Update log level dynamically when debug flag changes if s.cfg.Debug != cfg.Debug { - if cfg.Debug { - log.SetLevel(log.DebugLevel) - } else { - log.SetLevel(log.InfoLevel) - } + util.SetLogLevel(cfg) log.Debugf("debug mode updated from %t to %t", s.cfg.Debug, cfg.Debug) } diff --git a/internal/util/proxy.go b/internal/util/proxy.go index e23535a1..704b9fcb 100644 --- a/internal/util/proxy.go +++ b/internal/util/proxy.go @@ -1,6 +1,6 @@ // Package util provides utility functions for the CLI Proxy API server. // It includes helper functions for proxy configuration, HTTP client setup, -// and other common operations used across the application. +// log level management, and other common operations used across the application. package util import ( diff --git a/internal/util/util.go b/internal/util/util.go new file mode 100644 index 00000000..707b00cb --- /dev/null +++ b/internal/util/util.go @@ -0,0 +1,23 @@ +package util + +import ( + "github.com/luispater/CLIProxyAPI/internal/config" + log "github.com/sirupsen/logrus" +) + +// SetLogLevel configures the logrus log level based on the configuration. +// It sets the log level to DebugLevel if debug mode is enabled, otherwise to InfoLevel. +func SetLogLevel(cfg *config.Config) { + currentLevel := log.GetLevel() + var newLevel log.Level + if cfg.Debug { + newLevel = log.DebugLevel + } else { + newLevel = log.InfoLevel + } + + if currentLevel != newLevel { + log.SetLevel(newLevel) + log.Infof("log level changed from %s to %s (debug=%t)", currentLevel, newLevel, cfg.Debug) + } +} diff --git a/internal/watcher/watcher.go b/internal/watcher/watcher.go index 1915e93a..78d17af4 100644 --- a/internal/watcher/watcher.go +++ b/internal/watcher/watcher.go @@ -168,6 +168,14 @@ func (w *Watcher) reloadConfig() { w.config = newConfig w.clientsMutex.Unlock() + // Always apply the current log level based on the latest config. + // This ensures logrus reflects the desired level even if change detection misses. + util.SetLogLevel(newConfig) + // Additional debug for visibility when the flag actually changes. + if oldConfig != nil && oldConfig.Debug != newConfig.Debug { + log.Debugf("log level updated - debug mode changed from %t to %t", oldConfig.Debug, newConfig.Debug) + } + // Log configuration changes in debug mode if oldConfig != nil { log.Debugf("config changes detected:")