optimize code

This commit is contained in:
xing 2024-06-12 22:38:49 +08:00
parent d220bb2a6c
commit dcbe760f09

View File

@ -15,20 +15,24 @@ var logs = safety.NewVar[*log.Logger](nil)
var logFile = safety.NewVar[*os.File](nil) var logFile = safety.NewVar[*os.File](nil)
func InitLogger() error { func InitLogger() error {
c := config.GetConfig()
return SetLogger(c.LogOutput)
}
func SetLogger(loggerFile string) error {
if loggerFile == "" {
loggerFile = "stderr"
}
preFD := logFile.Load() preFD := logFile.Load()
l := &log.Logger{} l := &log.Logger{}
c := config.GetConfig()
if c.LogOutput == "" {
c.LogOutput = "stderr"
}
var out io.Writer var out io.Writer
switch c.LogOutput { switch loggerFile {
case "stdout": case "stdout":
out = os.Stdout out = os.Stdout
case "stderr": case "stderr":
out = os.Stderr out = os.Stderr
default: default:
file, err := os.OpenFile(c.LogOutput, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777) file, err := os.OpenFile(loggerFile, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777)
if err != nil { if err != nil {
return err return err
} }