go-ozzo/ozzo-log

Catching standard panics

Closed this issue · 3 comments

Hello.

Sorry for a newbie question, but how could I catch standard panics with this? For example I got this panic from Go:

2016/02/17 11:22:29 http: panic serving [::1]:58256: runtime error: index out of rang
e
goroutine 17 [running]:
net/http.(*conn).serve.func1(0xc0820b2000, 0x14110f8, 0xc0820ac018)
        c:/go/src/net/http/server.go:1287 +0xbc
main.whoisHandler(0x1411278, 0xc0820b20b0, 0xc0820b01c0)
        C:/Projects/test/main.go:121 +0xeb7
net/http.HandlerFunc.ServeHTTP(0x7d1de0, 0x1411278, 0xc0820b20b0, 0xc0820b01c0)
        c:/go/src/net/http/server.go:1422 +0x41
net/http.(*ServeMux).ServeHTTP(0xc08205a3f0, 0x1411278, 0xc0820b20b0, 0xc0820b01c0)
        c:/go/src/net/http/server.go:1699 +0x184
net/http.serverHandler.ServeHTTP(0xc0820a8000, 0x1411278, 0xc0820b20b0, 0xc0820b01c0)

        c:/go/src/net/http/server.go:1862 +0x1a5
net/http.(*conn).serve(0xc0820b2000)
        c:/go/src/net/http/server.go:1361 +0xbf5
created by net/http.(*Server).Serve
        c:/go/src/net/http/server.go:1910 +0x3fd

How could I log it properly and possibly redirect the output as well? Using the standard log library it's as simple as setting the log.SetOutput(logFile).

P.S. I know this has to be a bad error handling, but what if I missed something and such a panic arises.

This is a very legitimate question. I suggest you take a look at ozzo-routing or other similar HTTP routing packages. The idea is to use a middleware to "capture" panics (like shown here: https://github.com/go-ozzo/ozzo-routing/blob/master/fault/recovery.go#L38) and log them somewhere.

Thanks, I got this. By the way, any chance we could see benchmarks of ozzo-routing vs httprouter?

I only did the benchmark on my local repo using https://github.com/julienschmidt/go-http-routing-benchmark. The result is quite similar to that of httprouter since both are using the similar data structure for route matching. ozzo-routing has the advantage that it has zero dynamic memory allocation for both static and parametric routes.