feat: 更改 debug 模块为 log 模块 #11

This commit is contained in:
Mmx233
2022-09-01 14:31:11 +08:00
parent e90d58a506
commit 1b52a311dc
3 changed files with 33 additions and 28 deletions

View File

@@ -36,8 +36,8 @@ func readConfig() {
Guardian: srunModels.Guardian{
Duration: 300,
},
Debug: srunModels.Debug{
LogPath: "./",
Log: srunModels.Log{
FilePath: "./",
},
})

View File

@@ -9,27 +9,31 @@ import (
)
func initLog() {
if Config.Settings.Debug.Enable {
if Config.Settings.Log.DebugLevel {
log.SetLevel(log.DebugLevel)
}
if Config.Settings.Debug.WriteLog {
//日志路径初始化与处理
if !strings.HasSuffix(Config.Settings.Debug.LogPath, "/") {
Config.Settings.Debug.LogPath += "/"
}
e := os.MkdirAll(Config.Settings.Debug.LogPath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
f, e := os.OpenFile(Config.Settings.Debug.LogPath+time.Now().Format("2006.01.02-15.04.05")+".log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
if Config.Settings.Log.WriteFile {
//日志路径初始化与处理
if !strings.HasSuffix(Config.Settings.Log.FilePath, "/") {
Config.Settings.Log.FilePath += "/"
}
e := os.MkdirAll(Config.Settings.Log.FilePath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
if Config.Settings.Log.FileName == "" {
Config.Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log"
}
f, e := os.OpenFile(Config.Settings.Log.FilePath+Config.Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 700)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
}
}