go重定向输出崩溃堆栈
-
windows
// +build windows package log import ( "os" "syscall" "github.com/sirupsen/logrus" ) var ( crashFile *os.File kernel32 = syscall.MustLoadDLL("kernel32.dll") procSetStdHandle = kernel32.MustFindProc("SetStdHandle") ) func init() { var err error crashFile, err = os.OpenFile("./raysync-ftp_crash.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664) if err != nil { logrus.Fatalf("open raysync-ftp_crash.log file failure: %v", err) } handle := syscall.STD_ERROR_HANDLE syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(handle), crashFile.Fd(), 0) }
-
linux
// +build !windows package log import ( "os" "syscall" "github.com/sirupsen/logrus" ) var crashFile *os.File func init() { var err error crashFile, err = os.OpenFile("./raysync-ftp_crash.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0664) if err != nil { logrus.Fatalf("open raysync-ftp_crash.log file failure: %v", err) } err = syscall.Dup2(int(crashFile.Fd()), int(os.Stderr.Fd())) if err != nil { logrus.Fatalf("dup stderr fd failure: %v", err) } }