From a5e6ddc46a0306f630ac53a8e0e3843bf8c17a05 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期三, 07 四月 2021 16:43:09 +0800
Subject: [PATCH] 修复日志路径问题

---
 log/log.go |   21 ++++++++++++++++++---
 1 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/log/log.go b/log/log.go
index a6a39de..4a7e198 100644
--- a/log/log.go
+++ b/log/log.go
@@ -2,6 +2,9 @@
 
 import (
 	"io/ioutil"
+	"os"
+	"os/exec"
+	"path/filepath"
 	"time"
 
 	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
@@ -30,8 +33,8 @@
 	if Log != nil {
 		return Log
 	}
-
-	infopath := "logs/info.log"
+	rootPath := GetAppRootPath()
+	infopath := rootPath + "/logs/info.log"
 	infowriter, _ := rotatelogs.New(
 		infopath+".%Y%m%d%H%M",
 		rotatelogs.WithLinkName(infopath),
@@ -39,7 +42,7 @@
 		rotatelogs.WithRotationTime(24*time.Hour),
 	)
 
-	errorpath := "logs/error.log"
+	errorpath := rootPath + "/logs/error.log"
 	errorwriter, _ := rotatelogs.New(
 		errorpath+".%Y%m%d%H%M",
 		rotatelogs.WithLinkName(errorpath),
@@ -58,3 +61,15 @@
 	))
 	return Log
 }
+
+func GetAppRootPath() string {
+	file,err:= exec.LookPath(os.Args[0])
+	if err != nil {
+		return ""
+	}
+	p, err := filepath.Abs(file)
+	if err != nil {
+		return ""
+	}
+	return filepath.Dir(p)
+}

--
Gitblit v1.8.0