From f2d4474c59de2698c3290d567cbbb4fcbae52568 Mon Sep 17 00:00:00 2001 From: imbytecat Date: Fri, 23 Aug 2024 13:52:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(log):=20=E6=97=A5=E5=BF=97=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=96=87=E4=BB=B6=E8=B7=A8=E5=B9=B3=E5=8F=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 499d022..4188f41 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,8 @@ import ( "onvif-agent/integration/zabbixagent" "onvif-agent/router" "os" + "path/filepath" + "runtime" "time" ) @@ -18,17 +20,20 @@ func main() { * Logging */ date := time.Now().Format("2006-01-02") - f, err := os.OpenFile(fmt.Sprintf("/var/log/%s/%s.log", constant.AppName, date), os.O_RDWR|os.O_CREATE|os.O_APPEND, os.ModePerm) + logFile := fmt.Sprintf("%s.log", date) + var logFilePath string + if runtime.GOOS == "linux" { + logFilePath = fmt.Sprintf("/var/log/%s/%s", constant.AppName, logFile) + } else { + logFilePath = filepath.Join("log", logFile) + } + + f, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("Error opening file: %v", err) } - defer func(f *os.File) { - err := f.Close() - if err != nil { - log.Fatalf("Error closing file: %v", err) - } - }(f) + defer f.Close() writer := io.MultiWriter(os.Stdout, f) log.SetOutput(writer)