fix(log): 日志输出文件跨平台支持
This commit is contained in:
parent
a5f6c3b1d9
commit
f2d4474c59
19
main.go
19
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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user