History

Resolve `logsDir` path relative to configuration directory in `FileRequestLogger`

Luis Pater committed 1 year agov5.1.24

Commit 43758221 parent e0d13142 files · +11 −11Browse files

Unified · Split

2 files
  1. internal/api/server.go +3 −1
  2. internal/logging/request_logger.go +8 −10
internal/api/server.go+3−1
@@ -10,6 +10,7 @@ import ( · 9 unmodified lines
1010 "fmt"
1111 "net/http"
1212 "os"
13+ "path/filepath"
1314 "strings"
1415
1516 "github.com/gin-gonic/gin"
@@ -75,7 +76,8 @@ func NewServer(cfg *config.Config, cliClients []interfaces.Client, configFilePat · 59 unmodified lines
7576 engine.Use(gin.Recovery())
7677
7778 // Add request logging middleware (positioned after recovery, before auth)
78− requestLogger := logging.NewFileRequestLogger(cfg.RequestLog, "logs")
79+ // Resolve logs directory relative to the configuration file directory.
80+ requestLogger := logging.NewFileRequestLogger(cfg.RequestLog, "logs", filepath.Dir(configFilePath))
7981 engine.Use(middleware.RequestLoggingMiddleware(requestLogger))
8082
8183 engine.Use(corsMiddleware())
internal/logging/request_logger.go+8−10
@@ -98,20 +98,18 @@ type FileRequestLogger struct { · 97 unmodified lines
9898 //
9999 // Parameters:
100100 // - enabled: Whether request logging should be enabled
101−// - logsDir: The directory where log files should be stored
101+// - logsDir: The directory where log files should be stored (can be relative)
102+// - configDir: The directory of the configuration file; when logsDir is
103+// relative, it will be resolved relative to this directory
102104 //
103105 // Returns:
104106 // - *FileRequestLogger: A new file-based request logger instance
105−func NewFileRequestLogger(enabled bool, logsDir string) *FileRequestLogger {
106− // Resolve logsDir relative to the executable directory when it's not absolute.
107+func NewFileRequestLogger(enabled bool, logsDir string, configDir string) *FileRequestLogger {
108+ // Resolve logsDir relative to the configuration file directory when it's not absolute.
107109 if !filepath.IsAbs(logsDir) {
108− if exePath, err := os.Executable(); err == nil {
109− // Resolve symlinks to get the real executable path
110− if realExe, errEvalSymlinks := filepath.EvalSymlinks(exePath); errEvalSymlinks == nil {
111− exePath = realExe
112− }
113− execDir := filepath.Dir(exePath)
114− logsDir = filepath.Join(execDir, logsDir)
110+ // If configDir is provided, resolve logsDir relative to it.
111+ if configDir != "" {
112+ logsDir = filepath.Join(configDir, logsDir)
115113 }
116114 }
117115 return &FileRequestLogger{