mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
Resolve logsDir path relative to configuration directory in FileRequestLogger
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -75,7 +76,8 @@ func NewServer(cfg *config.Config, cliClients []interfaces.Client, configFilePat
|
||||
engine.Use(gin.Recovery())
|
||||
|
||||
// Add request logging middleware (positioned after recovery, before auth)
|
||||
requestLogger := logging.NewFileRequestLogger(cfg.RequestLog, "logs")
|
||||
// Resolve logs directory relative to the configuration file directory.
|
||||
requestLogger := logging.NewFileRequestLogger(cfg.RequestLog, "logs", filepath.Dir(configFilePath))
|
||||
engine.Use(middleware.RequestLoggingMiddleware(requestLogger))
|
||||
|
||||
engine.Use(corsMiddleware())
|
||||
|
||||
@@ -98,20 +98,18 @@ type FileRequestLogger struct {
|
||||
//
|
||||
// Parameters:
|
||||
// - enabled: Whether request logging should be enabled
|
||||
// - logsDir: The directory where log files should be stored
|
||||
// - logsDir: The directory where log files should be stored (can be relative)
|
||||
// - configDir: The directory of the configuration file; when logsDir is
|
||||
// relative, it will be resolved relative to this directory
|
||||
//
|
||||
// Returns:
|
||||
// - *FileRequestLogger: A new file-based request logger instance
|
||||
func NewFileRequestLogger(enabled bool, logsDir string) *FileRequestLogger {
|
||||
// Resolve logsDir relative to the executable directory when it's not absolute.
|
||||
func NewFileRequestLogger(enabled bool, logsDir string, configDir string) *FileRequestLogger {
|
||||
// Resolve logsDir relative to the configuration file directory when it's not absolute.
|
||||
if !filepath.IsAbs(logsDir) {
|
||||
if exePath, err := os.Executable(); err == nil {
|
||||
// Resolve symlinks to get the real executable path
|
||||
if realExe, errEvalSymlinks := filepath.EvalSymlinks(exePath); errEvalSymlinks == nil {
|
||||
exePath = realExe
|
||||
}
|
||||
execDir := filepath.Dir(exePath)
|
||||
logsDir = filepath.Join(execDir, logsDir)
|
||||
// If configDir is provided, resolve logsDir relative to it.
|
||||
if configDir != "" {
|
||||
logsDir = filepath.Join(configDir, logsDir)
|
||||
}
|
||||
}
|
||||
return &FileRequestLogger{
|
||||
|
||||
Reference in New Issue
Block a user