log api panic with a stack trace
parent
aabf82c965
commit
17a3a02ee3
|
@ -3,9 +3,14 @@ package http
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
|
|
||||||
platform "github.com/influxdata/influxdb"
|
platform "github.com/influxdata/influxdb"
|
||||||
|
influxlogger "github.com/influxdata/influxdb/logger"
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewRouter returns a new router with a 404 handler, a 405 handler, and a panic handler.
|
// 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),
|
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)
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue