Merge pull request #71 from dwdcth/v4

修复fatal.log不生成
This commit is contained in:
dexter
2023-02-13 16:53:28 +08:00
committed by GitHub
3 changed files with 8 additions and 6 deletions

View File

@@ -1,4 +1,4 @@
// +build linux,!arm64,darwin
//go:build (linux && !arm64) || darwin
package util
@@ -9,11 +9,12 @@ import (
)
func init() {
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Println("服务启动出错", "打开异常日志文件失败", err)
return
}
os.Stderr.WriteString("\n--------------------------------\n")
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
syscall.Dup2(int(logFile.Fd()), int(os.Stderr.Fd()))
}

View File

@@ -1,4 +1,4 @@
// +build linux,!darwin
//go:build linux && !darwin
package util
@@ -9,11 +9,12 @@ import (
)
func init() {
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
if err != nil {
log.Println("服务启动出错", "打开异常日志文件失败", err)
return
}
os.Stderr.WriteString("\n--------------------------------\n")
// 将进程标准出错重定向至文件,进程崩溃时运行时将向该文件记录协程调用栈信息
syscall.Dup3(int(logFile.Fd()), int(os.Stderr.Fd()), 0)
}

View File

@@ -1,5 +1,4 @@
//go:build windows
// +build windows
package util
@@ -27,11 +26,12 @@ func setStdHandle(stdhandle int32, handle syscall.Handle) error {
// redirectStderr to the file passed in
func init() {
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0666)
logFile, err := os.OpenFile("./fatal.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0666)
err = setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(logFile.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)
}
// SetStdHandle does not affect prior references to stderr
os.Stderr = logFile
os.Stderr.WriteString("\n--------------------------------\n")
}