जहाँ तक मुझे पता है, आप मानक त्रुटि से या अपने लॉगर से पैनिक से आउटपुट को रीडायरेक्ट नहीं कर सकते हैं। सबसे अच्छी बात यह है कि आप मानक त्रुटि को उस फ़ाइल में रीडायरेक्ट कर सकते हैं जिसे आप बाहरी रूप से या अपने प्रोग्राम के अंदर कर सकते हैं।
मेरे rclone प्रोग्राम के लिए मैंने एक विकल्प पर फ़ाइल में सबकुछ कैप्चर करने के लिए मानक त्रुटि को पुनर्निर्देशित किया है, दुर्भाग्य से क्रॉस प्लेटफ़ॉर्म तरीके से करना विशेष रूप से आसान नहीं है। यहाँ कैसे मैंने किया
लिनक्स के लिए (पुन: निर्देशन * .go फ़ाइलें देखें)/यूनिक्स
// Log the panic under unix to the log file
//+build unix
package main
import (
"log"
"os"
"syscall"
)
// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
err := syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)
}
}
और खिड़कियों
// Log the panic under windows to the log file
//
// Code from minix, via
//
// http://play.golang.org/p/kLtct7lSUg
//+build windows
package main
import (
"log"
"os"
"syscall"
)
var (
kernel32 = syscall.MustLoadDLL("kernel32.dll")
procSetStdHandle = kernel32.MustFindProc("SetStdHandle")
)
func setStdHandle(stdhandle int32, handle syscall.Handle) error {
r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0)
if r0 == 0 {
if e1 != 0 {
return error(e1)
}
return syscall.EINVAL
}
return nil
}
// redirectStderr to the file passed in
func redirectStderr(f *os.File) {
err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd()))
if err != nil {
log.Fatalf("Failed to redirect stderr to file: %v", err)
}
// SetStdHandle does not affect prior references to stderr
os.Stderr = f
}
स्रोत
2016-01-13 18:06:39
मुझे लगता है कि 'पुनर्प्राप्ति' आउटपुट को रीडायरेक्ट करने से बेहतर है। 'debug.PrintStack' आपको आवश्यकतानुसार लॉग प्रिंट कर सकता है। कुछ अन्य लॉग, उदाहरण के लिए, जीसी संदेश, अभी भी stderr में रह सकते हैं। – Bryce