From 7d950c0613cfac3b7e11f8bd58c21beccb124322 Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Tue, 23 Aug 2022 00:00:27 +0800 Subject: [PATCH] feat: log into file --- cmd/xproxy.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/cmd/xproxy.go b/cmd/xproxy.go index 66be71f..b83be84 100644 --- a/cmd/xproxy.go +++ b/cmd/xproxy.go @@ -6,12 +6,13 @@ import ( "XProxy/cmd/process" "flag" log "github.com/sirupsen/logrus" + "io" "os" "path" "strconv" ) -var version = "0.9.3" +var version = "0.9.4" var v4RouteTable = 104 var v6RouteTable = 106 var v4TProxyPort = 7288 @@ -23,8 +24,9 @@ var goVersion string var subProcess []*process.Process var assetDir, exposeDir, configFile string -func logInit(isDebug bool) { +func logInit(isDebug bool, logDir string) { log.SetFormatter(&log.TextFormatter{ + ForceColors: true, FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", }) @@ -32,13 +34,29 @@ func logInit(isDebug bool) { if isDebug { log.SetLevel(log.DebugLevel) } + logFile, err := os.OpenFile(path.Join(logDir, "xproxy.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644) + if err != nil { + log.Errorf("Unable to open log file -> %s", path.Join(logDir, "xproxy.log")) + } + log.SetOutput(io.MultiWriter(os.Stderr, logFile)) } func xproxyInit() { var isDebug = flag.Bool("debug", false, "Enable debug mode") var configName = flag.String("config", "xproxy.yml", "Config file name") flag.Parse() - logInit(*isDebug) + + exposeDir = "/xproxy" // default folder + if os.Getenv("EXPOSE_DIR") != "" { + exposeDir = os.Getenv("EXPOSE_DIR") + } + logInit(*isDebug, path.Join(exposeDir, "log")) + common.CreateFolder(exposeDir) + assetDir = path.Join(exposeDir, "assets") + configFile = path.Join(exposeDir, *configName) + log.Debugf("Expose folder -> %s", exposeDir) + log.Debugf("Assets folder -> %s", assetDir) + log.Debugf("Config file -> %s", configFile) if os.Getenv("IPV4_TABLE") != "" { v4RouteTable, _ = strconv.Atoi(os.Getenv("IPV4_TABLE")) @@ -56,17 +74,6 @@ func xproxyInit() { log.Debugf("IPv6 Route Table -> %d", v6RouteTable) log.Debugf("IPv4 TProxy Port -> %d", v4TProxyPort) log.Debugf("IPv6 TProxy Port -> %d", v6TProxyPort) - - exposeDir = "/xproxy" // default folder - if os.Getenv("EXPOSE_DIR") != "" { - exposeDir = os.Getenv("EXPOSE_DIR") - } - common.CreateFolder(exposeDir) - assetDir = path.Join(exposeDir, "assets") - configFile = path.Join(exposeDir, *configName) - log.Debugf("Expose folder -> %s", exposeDir) - log.Debugf("Assets folder -> %s", assetDir) - log.Debugf("Config file -> %s", configFile) } func main() {