log api panic with a stack trace

pull/11083/head
zhulongcheng 2019-01-07 12:12:41 +08:00
parent aabf82c965
commit 17a3a02ee3
1 changed files with 25 additions and 0 deletions

View File

@ -3,9 +3,14 @@ package http
import (
"fmt"
"net/http"
"os"
"runtime/debug"
"sync"
platform "github.com/influxdata/influxdb"
influxlogger "github.com/influxdata/influxdb/logger"
"github.com/julienschmidt/httprouter"
"go.uber.org/zap"
)
// NewRouter returns a new router with a 404 handler, a 405 handler, and a panic handler.
@ -50,5 +55,25 @@ func panicHandler(w http.ResponseWriter, r *http.Request, rcv interface{}) {
Err: fmt.Errorf("%v", rcv),
}
l := getPanicLogger()
l.Error(
pe.Msg,
zap.String("err", pe.Err.Error()),
zap.String("stack", fmt.Sprintf("%s", debug.Stack())),
)
EncodeError(ctx, pe, w)
}
var panicLogger *zap.Logger
var panicLoggerOnce sync.Once
// getPanicLogger returns a logger for panicHandler.
func getPanicLogger() *zap.Logger {
panicLoggerOnce.Do(func() {
panicLogger = influxlogger.New(os.Stderr)
panicLogger = panicLogger.With(zap.String("handler", "panic"))
})
return panicLogger
}