feat: 重构配置层,添加 json 支持

This commit is contained in:
Mmx233
2023-08-28 22:03:03 +08:00
parent 2a098e00dd
commit 6f283a93f5
19 changed files with 318 additions and 272 deletions

48
internal/config/log.go Normal file
View File

@@ -0,0 +1,48 @@
package config
import (
"io"
"os"
"strings"
"time"
nested "github.com/antonfisher/nested-logrus-formatter"
log "github.com/sirupsen/logrus"
)
func initLog() {
if Settings.Log.DebugLevel {
log.SetLevel(log.DebugLevel)
}
if Settings.Log.WriteFile {
//日志路径初始化与处理
if !strings.HasSuffix(Settings.Log.FilePath, "/") {
Settings.Log.FilePath += "/"
}
e := os.MkdirAll(Settings.Log.FilePath, os.ModePerm)
if e != nil {
log.Fatalln(e)
}
if Settings.Log.FileName == "" {
Settings.Log.FileName = time.Now().Format("2006.01.02-15.04.05") + ".log"
}
f, e := os.OpenFile(Settings.Log.FilePath+Settings.Log.FileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if e != nil {
log.Fatalln(e)
}
//设置双重输出
mw := io.MultiWriter(os.Stdout, f)
log.SetOutput(mw)
//设置输出格式
log.SetFormatter(&nested.Formatter{
HideKeys: true,
NoColors: Settings.Log.WriteFile,
TimestampFormat: "2006-01-02 15:04:05",
})
}
}