From 77a1ff1d29aab083ad1843e874434e43e7286b0c Mon Sep 17 00:00:00 2001 From: Grant Yin Date: Wed, 18 Dec 2019 21:53:13 +0800 Subject: [PATCH] Add check for log directory. --- QMPlusServer/init/qmlog/qmlog.go | 6 ++++++ QMPlusServer/tools/directory.go | 14 ++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 QMPlusServer/tools/directory.go diff --git a/QMPlusServer/init/qmlog/qmlog.go b/QMPlusServer/init/qmlog/qmlog.go index 5956ed0b..c7eceba6 100644 --- a/QMPlusServer/init/qmlog/qmlog.go +++ b/QMPlusServer/init/qmlog/qmlog.go @@ -6,6 +6,7 @@ import ( rotatelogs "github.com/lestrrat/go-file-rotatelogs" "github.com/rifflock/lfshook" "github.com/sirupsen/logrus" + "main/tools" "os" "time" ) @@ -19,6 +20,11 @@ func InitLog() *logrus.Logger{ } QMLog.Out = src QMLog.SetLevel(logrus.DebugLevel) + if ok, _ := tools.PathExists("./log"); !ok { + // Directory not exist + fmt.Println("Create log.") + os.Mkdir("log", os.ModePerm) + } apiLogPath := "./log/api.log" logWriter, err := rotatelogs.New( apiLogPath+".%Y-%m-%d-%H-%M.log", diff --git a/QMPlusServer/tools/directory.go b/QMPlusServer/tools/directory.go new file mode 100644 index 00000000..1f03da9d --- /dev/null +++ b/QMPlusServer/tools/directory.go @@ -0,0 +1,14 @@ +package tools + +import "os" + +func PathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err == nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +}