Skip to content

Commit ee17cfc

Browse files
committed
Refactor: enforce internal log redirection and log file path on startup
1 parent a10b5c4 commit ee17cfc

File tree

4 files changed

+17
-23
lines changed

4 files changed

+17
-23
lines changed

internal/app/install_windows.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"os"
88
"os/exec"
99
"path/filepath"
10-
"snirect/internal/config"
1110
"snirect/internal/logger"
1211
)
1312

@@ -22,14 +21,8 @@ func getBinPath() string {
2221

2322
func installServicePlatform(binPath string) error {
2423
taskName := "Snirect"
25-
logPath := config.GetDefaultLogPath()
2624

27-
// Ensure log directory exists
28-
if err := os.MkdirAll(filepath.Dir(logPath), 0755); err != nil {
29-
logger.Warn("Failed to create log directory: %v", err)
30-
}
31-
32-
cmd := exec.Command("schtasks", "/Create", "/TN", taskName, "/TR", fmt.Sprintf(`"cmd /c \"%s\" >> \"%s\" 2>&1"`, binPath, logPath),
25+
cmd := exec.Command("schtasks", "/Create", "/TN", taskName, "/TR", fmt.Sprintf(`"%s"`, binPath),
3326
"/SC", "ONLOGON", "/RL", "HIGHEST", "/F")
3427

3528
output, err := cmd.CombinedOutput()

internal/config/config.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#
2525
# 自动设置系统代理
2626
# 启动时自动将 Snirect 设置为系统代理。
27-
set_proxy = true
27+
# set_proxy = true
2828

2929
# [Root CA Installation]
3030
# Policy for automatically importing and trusting the Snirect Root CA.
@@ -118,10 +118,14 @@ set_proxy = true
118118
[log]
119119
# Log level: "DEBUG", "INFO", "WARN", "ERROR".
120120
# 日志级别: "DEBUG", "INFO", "WARN", "ERROR"。
121-
# loglevel = "DEBUG"
122-
# Path to the log file.
121+
# loglevel = "INFO"
122+
123123
# 日志文件路径。
124-
# logfile = "log.txt"
124+
# 如果留空,将使用以下默认系统路径:
125+
# Linux: ~/.local/state/snirect/snirect.log
126+
# macOS: ~/Library/Logs/snirect/snirect.log
127+
# Windows: %LOCALAPPDATA%\snirect\Logs\snirect.log
128+
# logfile = ""
125129

126130
# [Server Settings]
127131
# Configuration for the Snirect proxy server itself.

internal/config/loader.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ func LoadConfig(path string) (*Config, error) {
8989
return nil, fmt.Errorf("failed to parse default config: %w", err)
9090
}
9191

92-
// Set default log file path if empty (from defaults or user override)
93-
// We do it here initially in case user config doesn't exist
9492
if cfg.Log.File == "" {
9593
cfg.Log.File = GetDefaultLogPath()
9694
}

internal/logger/logger.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,27 @@ func updateLogger(path string) {
6262
}
6363
writers = append(writers, consoleWriter)
6464

65+
// Ensure directory exists if path is provided
6566
if path != "" {
6667
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
6768
fmt.Fprintf(os.Stderr, "Failed to create log directory: %v\n", err)
6869
}
6970
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
7071
if err == nil {
71-
// For file output, use standard JSON or Text handler for better machine readability, or simple text
72-
// Using basic text handler for file to keep it simple but structured
7372
writers = append(writers, f)
7473
} else {
7574
fmt.Fprintf(os.Stderr, "Failed to open log file: %v\n", err)
7675
}
7776
}
7877

79-
// We need a custom handler that writes formatted text to console and standard log to file
80-
// Since slog.New takes one handler, we'll wrap them using a MultiHandler approach
81-
// or simplify by just using our custom handler for console and writing directly to file if needed.
82-
// However, standard slog doesn't support multi-handlers out of the box easily without libraries.
83-
// Let's implement a simple wrapper handler.
84-
8578
handler := &MultiHandler{
8679
console: consoleWriter,
8780
file: nil,
8881
level: logLevel,
8982
}
9083

84+
// If a file writer was added, it's the second writer
9185
if len(writers) > 1 {
92-
// File writer is the second one
9386
handler.file = slog.NewTextHandler(writers[1], &slog.HandlerOptions{
9487
Level: logLevel,
9588
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
@@ -102,6 +95,12 @@ func updateLogger(path string) {
10295
}
10396

10497
currentLogger = slog.New(handler)
98+
99+
// Log the initial path info only if file logging is enabled
100+
if len(writers) > 1 {
101+
absPath, _ := filepath.Abs(path)
102+
currentLogger.Info(fmt.Sprintf("Logging to file: %s", absPath))
103+
}
105104
}
106105

107106
// MultiHandler dispatches to console (custom) and file (standard text)

0 commit comments

Comments
 (0)