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.

71 lines
1.4 KiB

package log
import (
"fmt"
"io"
"log"
"os"
"strconv"
)
var Log *log.Logger
// Init
//
// @Description: 初始化Logger
func Init() {
exists, _ := 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)
}
}
}
// pathExists
//
// @Description: 判断路径是否存在
// @param path
// @return bool
// @return error
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
}