logging - Golang: How to capture panic and log this error to original log file? -
i tried capture panic , log error:
func (s *server) sayhello(ctx context.context, in *pb.hellorequest) (*pb.helloreply, error) { defer func() { if err := recover(); err != nil { glog.errorf("recovered err: %v", err) } }() panic("tish panic") return &pb.helloreply{message: "hello " + in.name}, nil }
but surprise, "recovered err: "
never occurs in log file, instead, occurs in /var/log/messages
.
how can log error in original log file?
[updated]
if there no panic, glog.errorf
log correctly log dir; when there panic, can't:
// glog log correctly, unless uncomment panic below glog.errorf("this normal log: %v", err) // panic("tish panic")
maybe impossible, since that's crash means?
you need call glog.flush()
in deferred function. documentation of glog:
log output buffered , written periodically using flush. programs should call flush before exiting guarantee log output written.
Comments
Post a Comment