mirror of
https://github.com/SunBK201/UA3F.git
synced 2025-12-15 00:01:37 +00:00
refactor: update logging paths to use dynamic log directory
This commit is contained in:
parent
47bb5874f2
commit
87c290b60c
@ -10,8 +10,6 @@ PROG="/usr/bin/$NAME"
|
||||
start_service() {
|
||||
config_load "$NAME"
|
||||
|
||||
mkdir -p /var/log/ua3f && chmod o+w /var/log/ua3f
|
||||
|
||||
local enabled
|
||||
config_get_bool enabled "enabled" "enabled" "0"
|
||||
[ "$enabled" -eq "1" ] || return 0
|
||||
|
||||
@ -11,12 +11,10 @@ import (
|
||||
"gopkg.in/natefinch/lumberjack.v2"
|
||||
)
|
||||
|
||||
const log_file = "/var/log/ua3f/ua3f.log"
|
||||
|
||||
func SetLogConf(level string) {
|
||||
writer2 := os.Stdout
|
||||
writer3 := &lumberjack.Logger{
|
||||
Filename: log_file,
|
||||
Filename: GetLogFilePath(),
|
||||
MaxSize: 5, // megabytes
|
||||
MaxBackups: 5,
|
||||
MaxAge: 7, // days
|
||||
@ -58,6 +56,7 @@ func SetLogConf(level string) {
|
||||
func LogHeader(version string, cfg *config.Config) {
|
||||
slog.Info("UA3F started", "version", version, "", cfg)
|
||||
slog.Info("OS Info", GetOSInfo()...)
|
||||
slog.Info("Log file", "path", GetLogFilePath())
|
||||
}
|
||||
|
||||
func LogDebugWithAddr(src string, dest string, msg string) {
|
||||
|
||||
81
src/internal/log/path.go
Normal file
81
src/internal/log/path.go
Normal file
@ -0,0 +1,81 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
logDir string
|
||||
logDirOnce bool
|
||||
)
|
||||
|
||||
// GetLogDir returns the platform-specific log directory for UA3F.
|
||||
// - Linux: /var/log/ua3f/
|
||||
// - Windows: ~/.ua3f/
|
||||
// - Other (fallback): temp directory
|
||||
// The directory is created automatically with proper permissions if it doesn't exist.
|
||||
func GetLogDir() string {
|
||||
if logDirOnce {
|
||||
return logDir
|
||||
}
|
||||
|
||||
logDir = determineLogDir()
|
||||
logDirOnce = true
|
||||
|
||||
// Ensure the directory exists
|
||||
if err := os.MkdirAll(logDir, 0755); err != nil {
|
||||
// If creation fails, fall back to temp directory
|
||||
logDir = filepath.Join(os.TempDir(), "ua3f")
|
||||
_ = os.MkdirAll(logDir, 0755)
|
||||
}
|
||||
|
||||
return logDir
|
||||
}
|
||||
|
||||
func determineLogDir() string {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
// Try /var/log/ua3f/ first for Linux
|
||||
varLogDir := "/var/log/ua3f"
|
||||
if err := os.MkdirAll(varLogDir, 0755); err == nil {
|
||||
// Test write permission
|
||||
testFile := filepath.Join(varLogDir, ".write_test")
|
||||
if f, err := os.Create(testFile); err == nil {
|
||||
_ = f.Close()
|
||||
_ = os.Remove(testFile)
|
||||
return varLogDir
|
||||
}
|
||||
}
|
||||
// Fall through to user home directory
|
||||
return getUserLogDir()
|
||||
default:
|
||||
// macOS, Windows, FreeBSD, etc. - try user home first
|
||||
return getUserLogDir()
|
||||
}
|
||||
}
|
||||
|
||||
func getUserLogDir() string {
|
||||
// Try user home directory
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
userLogDir := filepath.Join(homeDir, ".ua3f")
|
||||
if err := os.MkdirAll(userLogDir, 0755); err == nil {
|
||||
return userLogDir
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to temp directory
|
||||
return filepath.Join(os.TempDir(), "ua3f")
|
||||
}
|
||||
|
||||
// GetLogFilePath returns the full path to the main log file.
|
||||
func GetLogFilePath() string {
|
||||
return filepath.Join(GetLogDir(), "ua3f.log")
|
||||
}
|
||||
|
||||
// GetStatsFilePath returns the full path to a stats file.
|
||||
func GetStatsFilePath(name string) string {
|
||||
return filepath.Join(GetLogDir(), name)
|
||||
}
|
||||
@ -1,5 +1,7 @@
|
||||
package statistics
|
||||
|
||||
import "github.com/sunbk201/ua3f/internal/log"
|
||||
|
||||
type Recorder struct {
|
||||
RewriteRecordList *RewriteRecordList
|
||||
PassThroughRecordList *PassThroughRecordList
|
||||
@ -8,9 +10,9 @@ type Recorder struct {
|
||||
|
||||
func New() *Recorder {
|
||||
return &Recorder{
|
||||
RewriteRecordList: NewRewriteRecordList("/var/log/ua3f/rewrite_stats"),
|
||||
PassThroughRecordList: NewPassThroughRecordList("/var/log/ua3f/pass_stats"),
|
||||
ConnectionRecordList: NewConnectionRecordList("/var/log/ua3f/conn_stats"),
|
||||
RewriteRecordList: NewRewriteRecordList(log.GetStatsFilePath("rewrite_stats")),
|
||||
PassThroughRecordList: NewPassThroughRecordList(log.GetStatsFilePath("pass_stats")),
|
||||
ConnectionRecordList: NewConnectionRecordList(log.GetStatsFilePath("conn_stats")),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user