kit icon indicating copy to clipboard operation
kit copied to clipboard

panic() is not working with logger

Open henriquechehad opened this issue 9 years ago • 5 comments

When happens the panic() during the application execution, it should let the panic happen, but instead of it the logger is avoiding the panic and just showing:

{"caller":"http: panic serving [::1","host":"localhost","msg":"]:65031: interface conversion: interface {} is string, not int","pid":9294,"ts":"2017/09/14 13:19:58","version":"20170914.201954"}

When the go-kit logger is not being used:

goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc42008de00)
	/usr/local/go/src/net/http/server.go:1721 +0xd0
panic(0x14fcb60, 0xc420191400)
	/usr/local/go/src/runtime/panic.go:489 +0x2cf

      ...

net/http.HandlerFunc.ServeHTTP(0xc42017e520, 0x1811100, 0xc4200e4700, 0xc42000b600)
	/usr/local/go/src/net/http/server.go:1942 +0x44
net/http.serverHandler.ServeHTTP(0xc420130b00, 0x1811100, 0xc4200e4700, 0xc42000b600)
	/usr/local/go/src/net/http/server.go:2568 +0x92
net/http.(*conn).serve(0xc42008de00, 0x1811940, 0xc420191200)
	/usr/local/go/src/net/http/server.go:1825 +0x612
created by net/http.(*Server).Serve
	/usr/local/go/src/net/http/server.go:2668 +0x2ce

henriquechehad avatar Sep 14 '17 20:09 henriquechehad

Are you using NewStdlibAdapter? The multi-line output of the panic is a challenge to parse in that context. See also https://github.com/go-kit/kit/issues/233.

ChrisHines avatar Sep 14 '17 20:09 ChrisHines

yes @ChrisHines,

Logger = kitlog.NewJSONLogger(kitlog.NewSyncWriter(os.Stdout))
Logger = kitlog.With(Logger, "ts", kitlog.DefaultTimestampUTC, "version", config.GetVersion(), "host", host, "pid", os.Getpid(), "caller", kitlog.DefaultCaller)
stdlog.SetOutput(kitlog.NewStdlibAdapter(Logger))

but at least it should let the panic happen

henriquechehad avatar Sep 14 '17 20:09 henriquechehad

The Go kit log packages don't recover any panics. The standard library net/http.Server does that.

ChrisHines avatar Sep 14 '17 20:09 ChrisHines

Ok, I think i see the problem, is needed to parse the panic message here: https://github.com/go-kit/kit/blob/master/log/stdlib.go#L68

Probably need regex changes. Or not use the option NewStdlibAdapter.

henriquechehad avatar Sep 14 '17 21:09 henriquechehad

No. I don't know how to solve the problem. The technique used by StdlibAdapter is to implement io.Writer and parse all the bytes written to it to extract structure and pass that to a go-kit log.Logger. That works great under the assumption that each log event is terminated by a single newline. But the output of a panic spans many lines and I don't know of a good way to know which line is the last one.

As an implementation of io.Writer we can't make any assumptions about how many calls to Write it will take. Logically we're receiving a stream of bytes and any newline could be the last, or there could be more coming.

ChrisHines avatar Sep 14 '17 22:09 ChrisHines