From 555410a9f91a85bc5c2447346aa6ce46ccc5a0d2 Mon Sep 17 00:00:00 2001 From: zhenorzz Date: Sun, 30 May 2021 20:21:18 +0800 Subject: [PATCH] A log stdout --- .env.example | 2 +- core/Log.go | 33 +++++++++++++++++++++------------ main.go | 4 ++-- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.env.example b/.env.example index 575950f..7754ab8 100644 --- a/.env.example +++ b/.env.example @@ -3,7 +3,7 @@ DB_CONN=root:xxxxxx@tcp(127.0.0.1:3306)/goploy?charset=utf8mb4,utf8 # password sign key SIGN_KEY=xxxxxxxxxxxxxx # sync log path -LOG_PATH=./tmp/ +LOG_PATH=stdout # rysnc and ssh key SSHKEY_PATH=~/.ssh/id_rsa # deploy enviorment diff --git a/core/Log.go b/core/Log.go index 61943f3..3e484df 100644 --- a/core/Log.go +++ b/core/Log.go @@ -2,9 +2,11 @@ package core import ( "fmt" + "io" "log" "os" "path/filepath" + "strings" "time" ) @@ -21,21 +23,28 @@ const ( // Log information to file with logged day func Log(lv LogLevel, content string) { - logPath, err := filepath.Abs(os.Getenv("LOG_PATH")) - if err != nil { - fmt.Println(err.Error()) - } - if _, err := os.Stat(logPath); err != nil && os.IsNotExist(err) { - err := os.Mkdir(logPath, os.ModePerm) + var logFile io.Writer + logPathEnv := os.Getenv("LOG_PATH") + if strings.ToLower(logPathEnv) == "stdout" { + logFile = os.Stdout + } else { + logPath, err := filepath.Abs(logPathEnv) + if err != nil { + fmt.Println(err.Error()) + } + if _, err := os.Stat(logPath); err != nil && os.IsNotExist(err) { + err := os.Mkdir(logPath, os.ModePerm) + if nil != err { + fmt.Println(err.Error()) + } + } + file := logPath + "/" + time.Now().Format("20060102") + ".log" + logFile, err = os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) if nil != err { fmt.Println(err.Error()) } } - file := logPath + "/" + time.Now().Format("20060102") + ".log" - logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) - if nil != err { - fmt.Println(err.Error()) - } - logger := log.New(logFile, string(lv), log.LstdFlags|log.Llongfile) + + logger := log.New(logFile, string(lv), log.LstdFlags|log.Lshortfile) logger.Output(2, content) } diff --git a/main.go b/main.go index 8441e6f..0f9d9d0 100644 --- a/main.go +++ b/main.go @@ -185,14 +185,14 @@ func install() { if len(mysqlPort) == 0 { mysqlPort = "3306" } - println("Please enter the absolute path of the log directory(default /tmp/):") + println("Please enter the absolute path of the log directory(default stdout):") logPath, err := inputReader.ReadString('\n') if err != nil { panic("There were errors reading, exiting program.") } logPath = utils.ClearNewline(logPath) if len(logPath) == 0 { - logPath = "/tmp/" + logPath = "stdout" } println("Please enter the listening port(default 80):") port, err := inputReader.ReadString('\n')