A log stdout

This commit is contained in:
zhenorzz 2021-05-30 20:21:18 +08:00
parent 3fc1be6f25
commit 555410a9f9
3 changed files with 24 additions and 15 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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')