From dcbe760f091fc081433c9b853c0c48041de22245 Mon Sep 17 00:00:00 2001 From: xing Date: Wed, 12 Jun 2024 22:38:49 +0800 Subject: [PATCH] optimize code --- app/pkg/logs/log.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/pkg/logs/log.go b/app/pkg/logs/log.go index b637c22..e7d7a2a 100644 --- a/app/pkg/logs/log.go +++ b/app/pkg/logs/log.go @@ -15,20 +15,24 @@ var logs = safety.NewVar[*log.Logger](nil) var logFile = safety.NewVar[*os.File](nil) func InitLogger() error { + c := config.GetConfig() + return SetLogger(c.LogOutput) +} + +func SetLogger(loggerFile string) error { + if loggerFile == "" { + loggerFile = "stderr" + } preFD := logFile.Load() l := &log.Logger{} - c := config.GetConfig() - if c.LogOutput == "" { - c.LogOutput = "stderr" - } var out io.Writer - switch c.LogOutput { + switch loggerFile { case "stdout": out = os.Stdout case "stderr": out = os.Stderr 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 { return err }