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
10 "fmt"10 "fmt"
11 "net/http"11 "net/http"
12 "os"12 "os"
13 "path/filepath"
13 "strings"14 "strings"
14 15
15 "github.com/gin-gonic/gin"16 "github.com/gin-gonic/gin"
@@ -75,7 +76,8 @@ func NewServer(cfg *config.Config, cliClients []interfaces.Client, configFilePat · 59 unmodified lines
75 engine.Use(gin.Recovery())76 engine.Use(gin.Recovery())
76 77
77 // Add request logging middleware (positioned after recovery, before auth)78 // 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))
79 engine.Use(middleware.RequestLoggingMiddleware(requestLogger))81 engine.Use(middleware.RequestLoggingMiddleware(requestLogger))
80 82
81 engine.Use(corsMiddleware())83 engine.Use(corsMiddleware())
internal/logging/request_logger.go+8−10
@@ -98,20 +98,18 @@ type FileRequestLogger struct { · 97 unmodified lines
98//98//
99// Parameters:99// Parameters:
100// - enabled: Whether request logging should be enabled100// - enabled: Whether request logging should be enabled
101// - logsDir: The directory where log files should be stored101// - 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
102//104//
103// Returns:105// Returns:
104// - *FileRequestLogger: A new file-based request logger instance106// - *FileRequestLogger: A new file-based request logger instance
105func NewFileRequestLogger(enabled bool, logsDir string) *FileRequestLogger {107func NewFileRequestLogger(enabled bool, logsDir string, configDir string) *FileRequestLogger {
106 // Resolve logsDir relative to the executable directory when it's not absolute.108 // Resolve logsDir relative to the configuration file directory when it's not absolute.
107 if !filepath.IsAbs(logsDir) {109 if !filepath.IsAbs(logsDir) {
108 if exePath, err := os.Executable(); err == nil {110 // If configDir is provided, resolve logsDir relative to it.
109 // Resolve symlinks to get the real executable path111 if configDir != "" {
110 if realExe, errEvalSymlinks := filepath.EvalSymlinks(exePath); errEvalSymlinks == nil {112 logsDir = filepath.Join(configDir, logsDir)
111 exePath = realExe
112 }
113 execDir := filepath.Dir(exePath)
114 logsDir = filepath.Join(execDir, logsDir)
115 }113 }
116 }114 }
117 return &FileRequestLogger{115 return &FileRequestLogger{