You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"expenses/util"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Log *log.Logger
|
|
|
|
|
|
|
|
// Init
|
|
|
|
//
|
|
|
|
// @Description: 初始化Logger
|
|
|
|
func Init() {
|
|
|
|
exists, _ := util.PathExists("./log")
|
|
|
|
if !exists {
|
|
|
|
_ = os.Mkdir("./log", 0755)
|
|
|
|
}
|
|
|
|
//SavePath := fmt.Sprintf("./log/log_%v.log", time.Now().Format("20060102_150405"))
|
|
|
|
SavePath := fmt.Sprintf("./log/log.log")
|
|
|
|
printConsole := true
|
|
|
|
for index, value := range os.Args {
|
|
|
|
if value == "-log_path" {
|
|
|
|
index += 1
|
|
|
|
SavePath = os.Args[index]
|
|
|
|
} else if value == "-log_ui" {
|
|
|
|
index += 1
|
|
|
|
va, err := strconv.Atoi(os.Args[index])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("cant parse -log_ui value ,current set value:", os.Args[index])
|
|
|
|
} else {
|
|
|
|
if va != 0 {
|
|
|
|
printConsole = true
|
|
|
|
} else {
|
|
|
|
printConsole = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logFile, er := os.OpenFile(SavePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766)
|
|
|
|
Log = log.New(logFile, "", log.Ltime|log.Lshortfile|log.LstdFlags)
|
|
|
|
if er != nil {
|
|
|
|
fmt.Printf("log file init faild! can't open file:%s ,error msg:%s", SavePath, er)
|
|
|
|
} else {
|
|
|
|
if printConsole {
|
|
|
|
mw := io.MultiWriter(os.Stdout, logFile)
|
|
|
|
Log.SetOutput(mw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|