From ed8c09655a43d9ec44142fb968e5abfbb69416cb Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期三, 19 五月 2021 15:48:14 +0800 Subject: [PATCH] fix --- log/log.go | 32 +++++++++++++++++++++++++++++--- 1 files changed, 29 insertions(+), 3 deletions(-) diff --git a/log/log.go b/log/log.go index 401c443..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" @@ -13,14 +16,25 @@ func init() { Log = NewLogger() + Log.SetLevel(logrus.ErrorLevel) +} + +func SetLogLevel(level string){ + if level == "ErrorLevel" { + Log.SetLevel(logrus.ErrorLevel) + } else if level == "InfoLevel" { + Log.SetLevel(logrus.InfoLevel) + } else { + Log.SetLevel(logrus.InfoLevel) + } } func NewLogger() *logrus.Logger { if Log != nil { return Log } - - infopath := "log/log_record/info.log" + rootPath := GetAppRootPath() + infopath := rootPath + "/logs/info.log" infowriter, _ := rotatelogs.New( infopath+".%Y%m%d%H%M", rotatelogs.WithLinkName(infopath), @@ -28,7 +42,7 @@ rotatelogs.WithRotationTime(24*time.Hour), ) - errorpath := "log/log_record/error.log" + errorpath := rootPath + "/logs/error.log" errorwriter, _ := rotatelogs.New( errorpath+".%Y%m%d%H%M", rotatelogs.WithLinkName(errorpath), @@ -47,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