Per quanto ne so, non è possibile reindirizzare l'uscita dal panico dall'errore standard o dal proprio registratore. La cosa migliore che puoi fare è redirigere l'errore standard in un file che puoi eseguire esternamente o all'interno del tuo programma.
Per il mio programma rclone ho reindirizzato l'errore standard per acquisire tutto su un file su un'opzione che sfortunatamente non è particolarmente facile da fare in modo multipiattaforma. Ecco come ho fatto (vedi il redirect * .Vai file)
per Linux/Unix
// 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)
}
}
e per le finestre
// 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
}
fonte
2016-01-13 18:06:39
Penso che 'recover' sia meglio di reindirizzare l'output. 'debug.PrintStack' può stampare i log di cui hai bisogno. Alcuni altri registri, ad esempio il messaggio GC, possono ancora rimanere nello stderr. – Bryce